屏幕旋转UIDeviceOrientation

屏幕旋转在播放器全屏,转屏的时候再次遇到。其实一些很简单的意思一开始有点糊涂,再总结下。网上的讲解也很多:
iOS屏幕旋转学习笔记
IOS UIDevice & IOS检测屏幕旋转实例
UIInterfaceOrientation和UIDeviceOrientation的区别
下面两个挺实用:
iOS屏幕旋转各类集锦(一)-单页旋转
iOS 关于屏幕旋转shouldAutorotate

自己做些测试

旋转效果

创建一个ViewController在self.view上直接add一个view, 橙色底子

BottomPlayView *bv = [[BottomPlayView alloc]init];
    bv.frame = CGRectMake(0, 50, 320, 240);
    bv.backgroundColor =[UIColor orangeColor];
    [self.view addSubview:bv];

然后转转屏:

rotate1.gif

往往,项目中的配置是这样的:

rotation.png

允许设备可以上左右的转屏。我们按竖屏样式像上面那样简单的写好frame,然后旋转屏幕时,视图控件也会自己转,会发现bv左边还是贴着屏幕边缘的,顶部也是有空隙,bv仍然按照之前写死的布局显示。我理解为,设备在转,UIDeviceOrientation在变化,而bv仍然一直想正对着我们, 也就是UIInterfaceOrientation也在变化(但是这个UIInterfaceOrientation怎么直接拿到,我还是不知道,希望知道的给我留言),你往左转,interface我就往右转一下,就产生了这样的效果。监听设备的旋转时

UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)orientation;

你会发现UIDeviceOrientationLandscapeLeft对应着UIInterfaceOrientationLandscapeRight在两者的枚举定义中也体现了这一点。

设备方向变化的时候,会发出 UIDeviceOrientationDidChangeNotification 通知,这样任何关心方向变化的view都可以通过注册该通知,在设备方向变化的时候做出相应的响应。UIKit会一级一级传递AppDelegate->Window->RootViewcontroller->other controller
我们项目中可以根据旋转来布局,还有些是不允许他乱转情况,慢慢捋

推出控制器请用模态的方式,不是push。

首先要开启旋转,可以像上图那样在项目中勾选,也可以AppDelegate中实现代理方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

我们要控制屏幕内容的旋转用到下面几个方法

//如果controller的内容需要旋转,就返回YES,否则返回NO,默认返回YES
-(BOOL)shouldAutorotate;
/*
当shouldAutorotate返回YES时调用;
屏幕发生旋转时,会在根视图控制器或者填充窗口的topmost presented控制器中调用这个方法,这里返回的方向,就是一个控制器支持的新的方向。
这个方法和系统配置的支持方向协同作用
*/
-(NSUInteger)supportedInterfaceOrientations;
/*
当你要模态推出一个控制器A时,返回希望得到A的interface方向
*/
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;

上面的三个方法很重要,文档说的也明白。
很多朋友有这样的需求:在一个控制器ViewControllerOne中present一个横屏viewcontrollerTwo,可是dismiss后发现One还是横着的,在One中设置了上面三个方法没有用,那就要检查下,这个One是不是导航控制器的根视图呢,导航是不是app的root呢?
tabbarController和navigationController有关系的。也就是你程序一开始的入口是谁控制的(你的工程的根试图控制器

  • 新建一个项目,就默认的配置,创建两个控制器,一个view往控制器上添加。我只说一点,后面放demo1。
    1、如果只勾选了Portrait,而在这个根视图中控制器中实现了下面的
//viewcontroller.m中

-(BOOL)shouldAutorotate{
    return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscapeLeft;
}//跑到这里就会崩溃,这与系统配置的不匹配

2、建议还是改回三个方向都支持,在需要的地方用代码控制就好。还有一个注意点,请看注释!

-(BOOL)shouldAutorotate{
    return YES;
}
//我们在这里支持Portrait
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}
//程序刚跑起来时,这个方法在这里么有用处,此控制器是根视图控制器,并非modal出来的,但是!dismiss回来后会调用!
//当然,这里写left也有问题,因为上面那个方法返回的是Portrait
//所以,如果需求当前是竖屏,这个方法直接不用实现。
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeLeft;
}
//好奇怪,加了导航后,此控制器做导航的root,这个方法不走了???
/**
程序的配置,supportedInterfaceOrientations,preferredInterfaceOrientationForPresentation有多种组合情况,大家自己试吧。
*/

结合起来,如果项目全程竖屏,只有一个页面横屏,那也可以只配置Portrait然后只在需要横屏的controller中设置下面一行代码就行。

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscapeLeft;
}
  • 新建一个工程,用storyboard拖了一个根视图控制器是navigationcontroller。
    这时候可以加一个分类
#import "UINavigationController+ScreenRotate.h"

@implementation UINavigationController (ScreenRotate)
-(BOOL)shouldAutorotate{
    return self.topViewController.shouldAutorotate;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return self.topViewController.supportedInterfaceOrientations;
}

@end

demo1
demo2

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,241评论 4 61
  • 在所有的实例讲解之前,我想先废话一下我对Rust的理解和想法。可能不是所有的人对Rust语言都有过了解。若已然了解...
    猿基地阅读 2,682评论 1 14
  • 一、简单的链接样式 a{color: red} 中的字体也会变红所以,最好使用伪类。a:link{}a:hove...
    Holase阅读 106评论 0 0
  • 某夜,我看到一档制作精良的谈话类节目<财富人生>,连着看了几期,感觉非常棒,由此也得到不少人生感悟。 生活中,并不...
    平安喜乐2阅读 420评论 0 0
  • 或者,你开心我们才能开心
    桃树的眼泪阅读 156评论 1 0