IOS UIDevice & IOS檢測屏幕旋轉實例
UIDevice類提供了一個單例實例代表當前的設備。從這個實例中可以獲得的信息設備,比如操作系統名稱、電池電量值(batteryLevel)、電池狀態(batteryState)、設備的類型(model,比如iPod、iPhone等)、設備的系統(systemVersion)
屏幕的旋轉朝向可以通過 [[UIDevice currentDevice]orientation] 判斷,orientation是個Integer類型,每個值表示相應的朝向,必須在調用beginGeneratingDeviceOrientationNotifications方法后,此orientation屬性才有效,否則一直是0。
// // ViewController.m // #import "ViewController.h" @interface ViewController () /** * UIImageView */ @property(nonatomic,strong)UIImageView *imageView; @end @implementation ViewController - (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation { //1.獲取 當前設備 實例 UIDevice *device = [UIDevice currentDevice] ; /** * 2.取得當前Device的方向,Device的方向類型為Integer * * 必須調用beginGeneratingDeviceOrientationNotifications方法后,此orientation屬性才有效,否則一直是0。orientation用于判斷設備的朝向,與應用UI方向無關 * * @param device.orientation * */ switch (device.orientation) { case UIDeviceOrientationFaceUp: NSLog(@"屏幕朝上平躺"); break; case UIDeviceOrientationFaceDown: NSLog(@"屏幕朝下平躺"); break; //系統無法判斷目前Device的方向,有可能是斜置 case UIDeviceOrientationUnknown: NSLog(@"未知方向"); break; case UIDeviceOrientationLandscapeLeft: NSLog(@"屏幕向左橫置"); break; case UIDeviceOrientationLandscapeRight: NSLog(@"屏幕向右橫置"); break; case UIDeviceOrientationPortrait: NSLog(@"屏幕直立"); break; case UIDeviceOrientationPortraitUpsideDown: NSLog(@"屏幕直立,上下顛倒"); break; default: NSLog(@"無法辨識"); break; } } - (void)viewDidLoad { //設備名稱 e.g. "My iPhone" NSString *strName = [[UIDevice currentDevice] name]; NSLog(@"設備名稱:%@", strName); /** * 系統名稱 e.g. @"iOS" */ NSString *strSysName = [[UIDevice currentDevice] systemName]; NSLog(@"系統名稱:%@", strSysName); /** * 系統版本號 e.g. @"4.0" */ NSString *strSysVersion = [[UIDevice currentDevice] systemVersion]; NSLog(@"系統版本號:%@", strSysVersion); /** * 設備類型 e.g. @"iPhone", @"iPod touch" */ NSString *strModel = [[UIDevice currentDevice] model]; NSLog(@"設備類型:%@", strModel); /** * 本地設備模式 localized version of model */ NSString *strLocModel = [[UIDevice currentDevice] localizedModel]; NSLog(@"本地設備模式:%@", strLocModel); /** * UUID 可用于唯一地標識該設備 */ NSUUID *identifierForVendor = [[UIDevice currentDevice] identifierForVendor]; NSLog(@"UUID:%@", identifierForVendor.UUIDString); /** * UIImage 對象 */ UIImage *image = [UIImage imageNamed:@"scroll.jpg"]; self.imageView.image = image; // 設置圖片范圍 CGFloat imageH = image.size.height; CGFloat imageW = image.size.width; CGFloat imageX = 0; CGFloat imageY = 0; self.imageView.frame = CGRectMake(imageX, imageY, imageW, imageH); [self.view addSubview:self.imageView]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } -(void)viewDidAppear:(BOOL)animated { /** * 開始生成 設備旋轉 通知 */ [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; /** * 添加 設備旋轉 通知 * * @param handleDeviceOrientationDidChange: handleDeviceOrientationDidChange: description * * @return return value description */ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDeviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil ]; } -(void)viewDidDisappear:(BOOL)animated { /** * 銷毀 設備旋轉 通知 * * @return return value description */ [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil ]; /** * 結束 設備旋轉通知 * * @return return value description */ [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma 懶加載 - (UIImageView *)imageView { if (!_imageView) { _imageView = [[UIImageView alloc] init]; } return _imageView; } @end
本文由用戶 y37f 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!