现在的位置: 首页 > 综合 > 正文

ios开发之确定设备当前的方向

2019年10月01日 ⁄ 综合 ⁄ 共 2412字 ⁄ 字号 评论关闭

有时候我们需要判断应用程序当前的方向,可以通过获取设备当前的方向来确定,从下面的定义你可以看到UIInterfaceOrientation的定义是通过UIDeviceOrientation来完成的,有两个概念:

UIDeviceOrientation:硬件设备的方向

UIInterfaceOrientation:应用程序界面的方向

UIDeviceOrientation的定义如下:

typedef enum {
    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
} UIDeviceOrientation;

UIInterfaceOrientation的定义如下:

typedef enum {
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;

可采用下面的方式来判断当前设备的方向:

UIDevice *device = [UIDevice currentDevice];
switch (device.orientation) {  
    case UIDeviceOrientationUnknown:  
        NSLog(@"Unknown");  
        break;
        
    case UIDeviceOrientationFaceUp:  
        NSLog(@"Device oriented flat, face up");  
        break;  
        
    case UIDeviceOrientationFaceDown:  
        NSLog(@"Device oriented flat, face down");  
        break;  
         
    case UIDeviceOrientationLandscapeLeft:  
        NSLog(@"Device oriented horizontally, home button on the right");  
        break;  
        
    case UIDeviceOrientationLandscapeRight:  
        NSLog(@"Device oriented horizontally, home button on the left");  
        break;  
        
    case UIDeviceOrientationPortrait:  
        NSLog(@"Device oriented vertically, home button on the bottom");  
        break;  
        
    case UIDeviceOrientationPortraitUpsideDown:  
        NSLog(@"Device oriented vertically, home button on the top");  
        break;  
        
    default:  
        NSLog(@"cannot distinguish");  
        break;  
} 

或者是:

UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;  
if (UIDeviceOrientationIsLandscape(deviceOrientation)) 
    NSLog(@"The orientation is landscape");      
else if(UIDeviceOrientationIsPortrait(deviceOrientation)) 
    NSLog(@"The orientation is portrait");

UIDeviceOrientationIsLandscape的定义如下:

#define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)

UIDeviceOrientationIsPortrait的定义如下:

#define UIDeviceOrientationIsPortrait(orientation)  ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)

有时候我们只需要知道当前设备是水平还是竖直放置,就可以采用第二种方法。


抱歉!评论已关闭.