美文网首页
屏幕旋转UIDeviceOrientation

屏幕旋转UIDeviceOrientation

作者: Liusr | 来源:发表于2017-06-05 16:58 被阅读298次

屏幕旋转在播放器全屏,转屏的时候再次遇到。其实一些很简单的意思一开始有点糊涂,再总结下。网上的讲解也很多:
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

相关文章

  • 屏幕旋转UIDeviceOrientation

    屏幕旋转在播放器全屏,转屏的时候再次遇到。其实一些很简单的意思一开始有点糊涂,再总结下。网上的讲解也很多:iOS屏...

  • UIDeviceOrientation与UIinterfaceO

    UIDeviceOrientation与UIinterfaceOrientation以及屏幕旋转的方式 在日常开发...

  • iOS 关于屏幕旋转问题

    关于屏幕旋转需要理解两个概念设备方向(UIDeviceOrientation)和屏幕方向(UIInterfaceO...

  • 如何使用代码处理 iOS 的横竖屏旋转问题!!!

    一、监听屏幕旋转方向 在处理iOS横竖屏时,经常会和UIDeviceOrientation、UIInterface...

  • iOS屏幕旋转(横竖屏)

    一、屏幕旋转方向监听 1、UIDeviceOrientation:设备方向 iOS 定义了七种设备方向: 当设备方...

  • iOS 屏幕旋转

    屏幕旋转 认知 期望达到的目的 如何让App支持屏幕旋转 如何让App屏幕旋转 如何保证屏幕旋转后布局不会乱 总结...

  • 屏幕旋转

    屏幕旋转 推荐文档 了解UIWindow——UIWindow实践 iOS屏幕旋转问题总结 IOS:屏幕旋转与变换 ...

  • iOS横竖屏切换

    基础概念 UIDeviceOrientation UIDeviceOrientation,表示设备朝向,可以通过[...

  • 屏幕旋转

    UIDevice.current.setValue(UIInterfaceOrientation.landscap...

  • 屏幕旋转

    import "AppDelegate.h" import "ViewController.h" @interfa...

网友评论

      本文标题:屏幕旋转UIDeviceOrientation

      本文链接:https://www.haomeiwen.com/subject/bctwfxtx.html