iOS开发中控制屏幕旋转的编写方法小结

所属分类: 软件编程 / IOS 阅读数: 547
收藏 0 赞 0 分享

在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,比如:

复制代码 代码如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

但是在iOS6中,这个方法被废弃了,使用无效。
shouldAutorotateToInterfaceOrientation:
Returns a Boolean value indicating whether the view controller supports the specified orientation. (Deprecated in iOS 6.0. Override the supportedInterfaceOrientations andpreferredInterfaceOrientationForPresentation methods instead.)

实践后会发现,通过supportedInterfaceOrientations的单独控制是无法锁定屏幕的。

复制代码 代码如下:

-(NSUInteger)supportedInterfaceOrientations 

    return UIInterfaceOrientationMaskPortrait; 

多次实验后总结出控制屏幕旋转支持方向的方法如下:
子类化UINavigationController,增加方法

复制代码 代码如下:

- (BOOL)shouldAutorotate 

    return self.topViewController.shouldAutorotate; 

 
- (NSUInteger)supportedInterfaceOrientations 

    return self.topViewController.supportedInterfaceOrientations; 


并且设定其为程序入口,或指定为 self.window.rootViewController
随后添加自己的view controller,如果想禁止某个view controller的旋屏:(支持全部版本的控制)
复制代码 代码如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

 
-(BOOL)shouldAutorotate 

    return NO; 

 
-(NSUInteger)supportedInterfaceOrientations 

    return UIInterfaceOrientationMaskPortrait; 

如果想又开启某个view controller的全部方向旋屏支持:

复制代码 代码如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
-(NSUInteger)supportedInterfaceOrientations 

    return UIInterfaceOrientationMaskAllButUpsideDown; 

 
-(BOOL)shouldAutorotate 

    return YES; 

从而实现了对每个view controller的单独控制。

顺便提一下,如果整个应用所有view controller都不支持旋屏,那么干脆:

复制代码 代码如下:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

     return UIInterfaceOrientationMaskPortrait; 


横竖屏切换,视图乱了怎么办?
首先,我们必须了解一下下列4种状态,它们被用来描述设备旋转方向:

2015102495412717.png (408×178)

对于旋屏的处理,大致分为如下几种情况和思路:
也许,你不需要旋屏支持,而希望锁定屏幕

复制代码 代码如下:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return NO; 


也许,你需要支持旋屏,或者支持部分方向的旋屏
复制代码 代码如下:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
       return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

也许,你的view有张背景图,旋屏时系统帮助你拉伸了图片,但是却没有管你的其它部件,比如button,你希望直接改变button的大小和位置

复制代码 代码如下:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) { 
        NSLog(@"现在是竖屏"); 
        [btn setFrame:CGRectMake(213, 442, 340, 46)]; 
    } 
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { 
        NSLog(@"现在是横屏"); 
        [btn setFrame:CGRectMake(280, 322, 460, 35)]; 
    } 


也许,你并不希望用绝对坐标去约束控件,而是希望让它通过旋转自己适应屏幕的旋转
复制代码 代码如下:

- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    UIDevice *device = [UIDevice currentDevice];  
    [device beginGeneratingDeviceOrientationNotifications]; 
    //利用 NSNotificationCenter 获得旋转信号 UIDeviceOrientationDidChangeNotification 
    NSNotificationCenter *ncenter = [NSNotificationCenter defaultCenter];  
    [ncenter addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:device]; 

 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
-(void)rotation_btn:(float)n 

    UIButton *robtn = self.btn; 
    robtn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0); 

 
-(void)orientationChanged 

    UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation]; 
     
    switch (orientaiton) { 
        caseUIDeviceOrientationPortrait:              
            [self  rotation_btn:0.0]; 
            break; 
        caseUIDeviceOrientationPortraitUpsideDown:   
            [self  rotation_btn:90.0*2]; 
            break; 
        caseUIDeviceOrientationLandscapeLeft:      
            [self  rotation_btn:90.0*3]; 
            break; 
        caseUIDeviceOrientationLandscapeRight:   
            [self  rotation_btn:90.0]; 
            break; 
        default: 
            break; 
    } 


也许,你需要autoresizesSubviews = YES
也许,你希望横竖屏有不同的布局效果,需要准备2份Subview,在不同状态去替换
当然不要忘记,需要调节设定图示中的1、2处,

2015102495438507.png (258×296)

来帮助我们完成自己想要的适应效果。Example 动画呈现的很清晰,^_^ 我就不再啰嗦了。

更多精彩内容其他人还在看

iOS 仿百度外卖-首页重力感应的实例

这篇文章主要介绍了iOS 仿百度外卖-首页重力感应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

iOS实现时间显示几分钟前,几小时前以及刚刚的方法示例

这篇文章主要介绍了如何利用iOS实现时间显示是在几小时前,几分钟前以及刚刚的格式,类似大家在qq空间和朋友圈微博看到的效果,文中给出了详细的示例代码,有需要的朋友们可以参考借鉴,下面来一起学习学习吧。
收藏 0 赞 0 分享

iOS 条码及二维码扫描(从相册中读取条形码/二维码)及扫码过程中遇到的坑

本文主要给大家介绍ios中从手机相册中读取条形码和二维码的问题及解决办法,需要的朋友参考下
收藏 0 赞 0 分享

IOS Cache设计详细介绍及简单示例

这篇文章主要介绍了IOS Cache设计详细介绍及简单示例的相关资料,Cache的目的是为了追求更高的速度体验,Cache的源头是两种数据读取方式在成本和性能上的差异,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS本地动态生成验证码的方法

这篇文章主要介绍了iOS本地动态生成验证码的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS绘制3D饼图的实现方法

饼图常用于统计学模块。常见的一般为2D饼图,这篇文章主要介绍了iOS绘制3D饼图的实现方法,3D饼图更加立体,用户的好感度也比较高,下面需要的朋友可以参考借鉴,一起来看看吧。
收藏 0 赞 0 分享

谈谈iOS开发之JSON格式数据的生成与解析

JSON格式取代了xml给网络传输带来了很大的便利,本篇文章主要介绍了iOS开发:对象直接转化成JSON详解,具有一定的参考价值,有兴趣的可以了解一下。
收藏 0 赞 0 分享

IOS 身份证校验详细介绍及示例代码

这篇文章主要介绍了IOS 身份证校验详细介绍及示例代码的相关资料,这里对身份校验比较详细,附有简单实例,需要的朋友可以参考下
收藏 0 赞 0 分享

IOS 自定义UICollectionView的头视图或者尾视图UICollectionReusableView

这篇文章主要介绍了IOS 自定义UICollectionView的头视图或者尾视图UICollectionReusableView的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

IOS 仿支付宝支付屏幕亮度变化机制

这篇文章主要介绍了IOS 仿支付宝支付屏幕亮度变化机制的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多