码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-11-12 23:13:34      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:

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 - Cant 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

 

获取设备方向和用户界面方向

 获取设备方向UIDeviceOrientation

[[UIDevice currentDevice] orientation]

获取用户界面方向 UIInterfaceOrientation

[[UIApplication sharedApplication] statusBarOrientation] 


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

// 设备方向改变

 
[[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

 

 

 

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

标签:

原文地址:http://www.cnblogs.com/sueZheng/p/4960395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!