iOS设备方向和用户界面方向

UIDeviceOrientation refers to the physical orientation of the device whereas UIInterfaceOrientation refers to the orientation of the user interface.

UIDeviceOrientation is a property of the UIDevice class, and there are these possible values:

UIDeviceOrientationUnknown - Can't be determined 
UIDeviceOrientationPortrait - Home button facing down
UIDeviceOrientationPortraitUpsideDown  - Home button facing up
UIDeviceOrientationLandscapeLeft - Home button facing right
UIDeviceOrientationLandscapeRight - Home button facing left
UIDeviceOrientationFaceUp - Device is flat, with screen facing up
UIDeviceOrientationFaceDown - Device is flat, with screen facing down

As for UIInterfaceOrientation, it is a property of UIApplication and only contains 4 possibilities which correspond to the orientation of the status bar:

UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,

UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,

UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,

UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft

 

获取设备方向/用户界面方向/controller方向

 获取设备方向UIDeviceOrientation

[[UIDevice currentDevice] orientation]

获取用户界面方向 UIInterfaceOrientation

[[UIApplication sharedApplication] statusBarOrientation] 

 

需要注意UIDeviceOrientation只是设备物理方向状态,至于应用中是方向还是要以UIInterfaceOrientation为准。用UIDeviceOrientation的时候,UIDeviceOrientationFaceUp、UIDeviceOrientationFaceDown,平放朝上和朝下的处理有问题,会出错。

获取特定controller的方向

controller.interfaceOrientation
区别: http://stackoverflow.com/questions/7968451/different-ways-of-getting-current-interface-orientation

设备和用户界面方向改变的监听

// 设备方向改变

 
[[NSNotificationCenter defaultCenter] addObserver:self                     
                                         selector:@selector(handleOrientChange:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];
​
- (void)orientationChanged:(NSNotification *)notification{
   [self adjustViewsForOrientation:[[UIApplication sharedApplication]
                                    statusBarOrientation]];
}
​
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
​
    switch (orientation)
    {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
        { 
        //load the portrait view    
        }
​
            break;
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
        {
        //load the landscape view 
        }
            break;
        case UIInterfaceOrientationUnknown:break;
    }
}
​

// 用户界面方向改变

[[NSNotificationCenter defaultCenter] addObserver:self                  selector:@selector(didChangeOrientation:)                                                    name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
​
- (void)didChangeOrientation:(NSNotification *)notification
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
​
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        NSLog(@"Landscape");
    }
    else {
        NSLog(@"Portrait")
    }
}

用NSNotificationCenter记得在dealloc方法中remove观察者

[[NSNotificationCenter defaultCenter] removeObserver:self];

 

判断是否竖屏

BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);

 

判断横竖屏

BOOL isLandscape = self.view.frame.size.width > self.view.frame.size.height;

 

当controllers不是全屏时,这种方法有问题。

 

作者:sue_zheng

出处:http://www.cnblogs.com/sueZheng/p/4954938.html

 

 

 

posted @ 2015-11-12 21:54  sue_zheng  Views(1900)  Comments(0Edit  收藏  举报