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

有时候我们需要判断应用程序当前的方向,可以通过获取设备当前的方向来确定,从下面的定义你可以看到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)

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值