1、UIDeviceOrientation 设备的物理方向
-
简介
UIDeviceOrientation即我们手持的移动设备的Orientation,是一个三围空间,故有六个方向:
-
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
-
获取
通过[UIDevice currentDevice].orientation获取当前设备的方向
当关闭了系统的横竖屏切换开关,即系统层级只允许竖屏时,再通过上述方式获取到的设备方向将只是UIDeviceOrientationPortrait。
-
UIDeviceOrientation是硬件设备的方向,是随着硬件自身改变的,只能取值,不能设置。
- typedef enum {
- UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
- UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
- UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
- UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
- } UIInterfaceOrientation;
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- //宣告一個UIDevice指標,並取得目前Device的狀況
- UIDevice *device = [UIDevice currentDevice] ;
- //取得當前Device的方向,來當作判斷敘述。(Device的方向型態為Integer)
- 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;
- }
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); // 只支持向左横向, YES 表示支持所有方向
- }
-
2、UIInterfaceOrientation界面的显示方向
-
-
简介
UIInterfaceOrientation即我们看到的视图的Orientation,可以理解为statusBar所在的方向,是一个二维空间,有四个方向:
-
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft