一、两种方向的区别
- 设备方向
设备的物理方向,由类型UIDeviceOrientation表示,当前设备方向获取方式:
[UIDevice currentDevice].orientation
该属性的值一般是与当前设备方向保持一致的,但须注意以下几点:
①文档中对该属性的注释
@property(nonatomic,readonly) UIDeviceOrientation orientation; // return current device orientation. this will return UIDeviceOrientationUnknown unless device orientation notifications are being generated.
哈哈哈...哈哈哈,英语好的可以自行翻译,英语的小白可以参考如下翻译或者打开谷歌翻译(切记,千万别用百度翻译,你懂得...),翻译如下:返回当前设备方向。 除非打开方向通知,否则这将返回UIDeviceOrientationUnknown
鉴于以上原因,可以使用下面方法代替:
if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
NSLog(@"%d",[UIDevice currentDevice].orientation);
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
**②系统横竖屏开关关闭
如果关闭了系统的横竖屏切换开关,即系统层级只允许竖屏时间,再通过上述方式获取到的设备方向将永远是UIDeviceOrientationUnknown可以通过Core Motion中的CMMotionManager来获取当前设备方向。 - 界面方向
界面显示的方向,由类型UIInterfaceOrientation表示。当前界面显示方向有以下两种方式获取:
NSLog(@"%d",[UIApplication sharedApplication].statusBarOrientation);
NSLog(@"%d",viewController.interfaceOrientation); - 区别
通过UIDevice获取到的设备方向在手机旋转时是实时的,通过UIApplication的statusBar或者viewController获取到界面方向在下述方法:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:
调用以后才会被更改成最新的值。
二、相关枚举定义 - UIDeviceOrientation:
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top
UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left
UIDeviceOrientationFaceUp, // Device oriented flat, face up
UIDeviceOrientationFaceDown // Device oriented flat, face down
};
2 . UIInterfaceOrientation:
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
};
同样,英语不好的童鞋,please open google (装逼一下,哈哈...哈哈)........此处略去5千字.........
从宏定义可知,设备方向比界面多了两个定义:UIDeviceOrientationFaceUp和UIDeviceOrientationFaceDown,分别表示手机水平放置时,屏幕向上和屏幕向下。
网友评论