检测手机的屏幕方向:
一、在需要检测的页面里的ViewDidLoad方法里注册通知:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDeviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
二、检测设备方向的回调方法
- (void)handleDeviceOrientationDidChange: (UIInterfaceOrientation)interfaceOrientation {
UIDevice *device = [UIDevice currentDevice] ;
switch (device.orientation) {
case UIDeviceOrientationFaceUp:
NSLog(@"屏幕朝上平躺");
break;
case UIDeviceOrientationFaceDown:
NSLog(@"屏幕朝下平躺");
break;
case UIDeviceOrientationUnknown:
NSLog(@"未知方向");
break;
case UIDeviceOrientationLandscapeLeft: {
NSLog(@"屏幕向左横置");
}
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"屏幕向右橫置");
break;
case UIDeviceOrientationPortrait: {
NSLog(@"屏幕直立");
}
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"屏幕直立,上下顛倒");
break;
default:
NSLog(@"无法辨识");
break;
}
}
三、注销 Device
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
}