前些日子处理了一些关于页面中的屏幕旋转的事情,需要根据某个页面的不同旋转方向去做不同的处理,现在再次总结一下,以在心里有个系统的体系,也可以当成工具文章查询。
首先来看第一个方法:
[UIDevice currentDevice].orientation
可以看到这个属性返回的是设备的物理方向,并且我们发现这个属性是只读的, UIDeviceOrientation 是硬件设备的方向,这个方向是随着硬件自身改变而改变的,只能取值,不能赋值。
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
UIDeviceOrientationUnknown, //值是0,以下依次递增1
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 //设备屏幕朝下
} __TVOS_PROHIBITED;
上面是UIDeviceOrientation的枚举,我们的设备处于三维空间中,因此有6个方向。
我们可以通过添加:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(deviceChange)
name:UIDeviceOrientationDidChangeNotification object:nil];
UIDeviceOrientationDidChangeNotification
这个通知来监听当前设备的物理方向:
-(void)deviceChange{
NSLog(@"设备旋转");
NSLog(@"当前设备方向:%ld",[UIDevice currentDevice].orientation);
}
接着来看第二个方法:
[UIApplication sharedApplication].statusBarOrientation
从上图中我们可以看到,这个值返回的是当前app的status bar的方向,即状态栏的方向