首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS屏幕旋转及其基本适配方法

iOS屏幕旋转及其基本适配方法

作者头像
梧雨北辰
修改2018-08-13 10:22:02
8.8K1
修改2018-08-13 10:22:02
举报

屏幕旋转示例.jpeg

前段时间抽空总结了一下iOS视频播放的基本用法,发现这其中还有一个我们无法绕过的问题,那就是播放界面的旋转与适配。的确,视频播放与游戏类型的App经常会遇到这个的问题。由于至今接手的项目中不常涉及这块知识疏于总结,在搜索了一些资料后也发现都很散乱,所以决定在这里重新整理一下。

目录

一、最让人纠结的三种枚举 二、两种屏幕旋转的触发方式 三、屏幕旋转控制的优先级 四、开启屏幕旋转的全局权限 五、开启屏幕旋转的局部权限(视图控制器) 六、实现需求:项目主要界面竖屏,部分界面横屏 七、默认横屏无效的问题 八、关于旋转后的适配问题 九、APP启动即全屏

一、最让人纠结的三种枚举

刚开始接触屏幕旋转这块知识的时候,最让人抓狂的也许就是三种相关的枚举类型了,它们就是UIDeviceOrientation、UIInterfaceOrientation、UIInterfaceOrientationMask。下面我们针对三种属性进行解析:

1. 设备方向:UIDeviceOrientation

UIDeviceOrientation是硬件设备(iPhone、iPad等)本身的当前旋转方向,设备方向有7种(包括一种未知的情况),判断设备的方向是以home键的位置作为参照的,我们来看一下它们在源码中的定义如下:

//Portrait 表示纵向,Landscape 表示横向。
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {

     UIDeviceOrientationUnknown,

     UIDeviceOrientationPortrait,           // Device oriented vertically, home button on the bottom

     UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top

     UIDeviceOrientationLandscapeLeft,      // Device oriented horizontally, home button on the right

     UIDeviceOrientationLandscapeRight,     // Device oriented horizontally, home button on the left

     UIDeviceOrientationFaceUp,             // Device oriented flat, face up

     UIDeviceOrientationFaceDown            // Device oriented flat, face down

   } __TVOS_PROHIBITED;

设备方向只能取值,不能设置, 获取设备当前旋转方向使用方法:[UIDevice currentDevice].orientation 监测设备方向的变化,我们可以在Appdelegate文件中使用通知如下:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDeviceOrientationDidChange)
                     name:UIDeviceOrientationDidChangeNotification
                                               object:nil];

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

 - (BOOL)onDeviceOrientationDidChange{
    //获取当前设备Device
    UIDevice *device = [UIDevice currentDevice] ;
    //识别当前设备的旋转方向
    switch (device.orientation) {
        case UIDeviceOrientationFaceUp:
            NSLog(@"屏幕幕朝上平躺");
            break;

        case UIDeviceOrientationFaceDown:
            NSLog(@"屏幕朝下平躺");
            break;

        case UIDeviceOrientationUnknown:
            //系统当前无法识别设备朝向,可能是倾斜
            NSLog(@"未知方向");
            break;

        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"屏幕向左橫置");
            break;

        case UIDeviceOrientationLandscapeRight:
            NSLog(@"屏幕向右橫置");
            break;

        case UIDeviceOrientationPortrait:
            NSLog(@"屏幕直立");
            break;

        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"屏幕直立,上下顛倒");
            break;

        default:
            NSLog(@"無法识别");
            break;
    }
    return YES;
}

2.页面方向:UIInterfaceOrientation

UIInterfaceOrientation程序界面的当前旋转方向(可以设置),其源码的定义如下:

    // Note that UIInterfaceOrientationLandscapeLeft is equal to UIDeviceOrientationLandscapeRight (and vice versa).
    // This is because rotating the device to the left requires rotating the content to the right.
    typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {

        UIInterfaceOrientationUnknown               = UIDeviceOrientationUnknown,

        UIInterfaceOrientationPortrait              = UIDeviceOrientationPortrait,

        UIInterfaceOrientationPortraitUpsideDown    = UIDeviceOrientationPortraitUpsideDown,

        UIInterfaceOrientationLandscapeLeft         = UIDeviceOrientationLandscapeRight,

        UIInterfaceOrientationLandscapeRight        = UIDeviceOrientationLandscapeLeft

    } __TVOS_PROHIBITED;

区别与UIDeviceOrientation,表示我们开发的程序界面的方向使用UIInterfaceOrientation。 值得注意的是:

UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, 
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft

我们可以发现两者的枚举值大多是可以对应上的。只有左右旋转的时候是UIInterfaceOrientationLandscapeLeft 与UIDeviceOrientationLandscapeRight相等,反之亦然,这是因为向左旋转设备需要旋转程序界面右边的内容。

3.页面方向:UIInterfaceOrientationMask

UIInterfaceOrientationMask是iOS6之后增加的一种枚举,其源码如下:

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {

    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),

    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),

    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),

    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),

    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),

    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),

    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),

} __TVOS_PROHIBITED;

我们知道UIDeviceOrientation与UIInterfaceOrientation的区别在于:前者是真实的设备方向,后者是页面方向。 而UIInterfaceOrientation和UIInterfaceOrientationMask的区别是什么呢?其实观察源码,我们就会发现这是一种为了支持多种UIInterfaceOrientation而定义的类型。下面的示例将很好的说明这点:

在iOS6之后,控制单个界面的旋转我们通常是下面三个方法来控制:

//方法1
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
//方法2
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
// Returns interface orientation masks.
//方法3
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;

方法2的作用是设置当前界面支持的所有方向,所以返回值是UIInterfaceOrientationMask,更加方便的表达支持多方向旋转的情况。

方法3作用是设置进入界面默认支持的方向,使用了返回值类型UIInterfaceOrientation,默认进入界面的方向是个确定的方向,所以使用UIInterfaceOrientation更适合。

二、两种屏幕旋转的触发方式

我们开发的App的,大多情况都是大多界面支持竖屏,几个特别的界面支持旋转横屏,两种界面相互切换,触发其旋转有两种情况:

情况1:系统没有关闭自动旋转屏幕功能,

这种情况,支持旋转的界面跟随用户手持设备旋转方向自动旋转。我们需要在当前视图控制器中添加如下方法:

//1.决定当前界面是否开启自动转屏,如果返回NO,后面两个方法也不会被调用,只是会支持默认的方向
- (BOOL)shouldAutorotate {
      return YES;
}

//2.返回支持的旋转方向
//iPad设备上,默认返回值UIInterfaceOrientationMaskAllButUpSideDwon
//iPad设备上,默认返回值是UIInterfaceOrientationMaskAll
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
     return UIInterfaceOrientationMaskAll;
}

//3.返回进入界面默认显示方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
     return UIInterfaceOrientationPortrait;
}

情况2:单个界面强制旋转

在程序界面通过点击等方式切换到横屏(尤其是视频播放的情况),有以下两种方法:

// 方法1:
- (void)setInterfaceOrientation:(UIDeviceOrientation)orientation {
      if ([[UIDevice currentDevice]   respondsToSelector:@selector(setOrientation:)]) {
          [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:orientation]     
                                       forKey:@"orientation"];
        }
    }

// 方法2:
- (void)setInterfaceOrientation:(UIInterfaceOrientation)orientation {
   if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            SEL selector = NSSelectorFromString(@"setOrientation:");
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice     
        instanceMethodSignatureForSelector:selector]];
            [invocation setSelector:selector];
            [invocation setTarget:[UIDevice currentDevice]];
            int val = orientation;
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
        }
    }

注意:使用这两个方法的时候,也要确保shouldAutorotate方法返回YES,这样这两个方法才会生效。还要注意两者使用的参数类型不同。

三、屏幕旋转控制的优先级

事实上,如果我们只用上面的方法来控制旋转的开启与关闭,并不能符合我们的需求,而且方法无效。这是因为我们忽略了旋转权限优先级的问题。关于屏幕旋转的设置有很多,有Xcode的General设置,也有info.plist设置,更还有代码设置等,这么多的设置很是繁杂。但是这些其实都是在不同级别上实现旋转的设置,我们会遇到设置关闭旋转无效的情况,这就很可能是被上一级别控制的原因。

我们首先有个大致的了解,控制屏幕旋转优先级为:工程Target属性配置(全局权限) = Appdelegate&&Window > 根视图控制器> 普通视图控制器。

四、开启屏幕旋转的全局权限

这里我使用全局权限来描述这个问题可能不太准确,其实是设置我们的设备能够支持的方向有哪些,这也是实现旋转的前提。 开启屏幕旋转的全局权限有三种方法,包括通过Xcode直接配置的两种方法和代码控制的一种方法。这三种方法作用相同,但是由于代码的控制在程序启动之后,所以也是最有效的。下面分别对三种方法的用法介绍:

1.Device Orientation属性配置

我们创建了新工程,Xcode就默认替我们选择了支持旋转的几个方向,这就是Device Orientation属性的默认配置。在Xcode中依次打开:【General】—>【Deployment Info】—>【Device Orientation】,我们可以看到默认支持的设备方向如下:

image.png

可以发现,UpsideDown没有被默认支持,因为对于iPhone即使勾选也没有UpSideDown的旋转效果。我们可以在这里勾选或者取消以修改支持的旋转方向。如果是iPad设备勾选之后会同时支持四个方向。

值得注意的是,对于iPhone,如果四个属性我们都选或者都不选,效果和默认的情况一样。

2.Info.Plist设置

其实我们设置了Device Orientation之后,再到info.plist中查看Supported interface orientation,我们会看到:

屏幕快照 2018-01-11 下午5.27.53.png

没错,此时Supported interface orientation里的设置和UIDevice Orientation的值一致的,并且我们在这里增加或者删除其中的值,UIDevice Orientation的值也会随之变化,两者属于同一种设置。

3.Appdelegate&&Window中设置

正常情况下,我们的App从Appdelegate中启动,而Appdelegate所持有唯一的Window对象是全局的,所以在Appdelegate文件中设置屏幕旋转也是全局有效的。下面的代码设置了只支持竖屏和右旋转:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    return  UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;

}

值得注意的是:如果我们实现了Appdelegate的这一方法,那么我们的App的全局旋转设置将以这里的为准,即使前两种方法的设置与这里的不同。

五、开启屏幕旋转的局部权限(视图控制器)

在设置了全局所支持的旋转方向后,接着就开始设置具体的控制器界面了。我们在上面已经说明了关于旋转的优先级了。而这里主要涉及了三种视图控制器(UITabbarViewController,UINavigationBarController ,UIViewController)

自全局权限开启之后,接下来具有最高权限的就是Window的根视图控制器rootViewController了。如果我们要具体控制单个界面UIViewController的旋转就必须先看一下根视图控制器的配置情况了。

当然,在一般情况下,我们的项目都是用UITabbarViewController作为Window的根视图控制器,然后管理着若干个导航控制器UINavigationBarController,再由导航栏控制器去管理普通的视图控制器UIViewController。若以此为例的话,关于旋转的优先级从高到低就是UITabbarViewController>UINavigationBarController >UIViewController了。如果具有高优先级的控制器关闭了旋转设置,那么低优先级的控制器是无法做到旋转的。

比如说我们设置要单个视图控制器可以自动旋转,这需要在视图控制器中增加shouldAutorotate方法返回YES或者NO来控制。但如果存在上层根视图控制器,而我们只在这个视图控制器中实现方法,会发现这个方法是不走的,因为这个方法被上层根视图控制器拦截了。理解这个原理后,我们有两种方法实现自动可控的旋转设置。

方法1:逐级设置各视图控制器,高优先级的视图控制器影响低优先级控制器,

解决上述的问题我们需要设置UITabbarViewController如下:

//是否自动旋转
-(BOOL)shouldAutorotate{
    return self.selectedViewController.shouldAutorotate;
}

//支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.selectedViewController supportedInterfaceOrientations];
}

//默认方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

设置导航控制器UINavigationController如下:

//是否自动旋转
//返回导航控制器的顶层视图控制器的自动旋转属性,因为导航控制器是以栈的原因叠加VC的
//topViewController是其最顶层的视图控制器,
-(BOOL)shouldAutorotate{
    return self.topViewController.shouldAutorotate;
}

//支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}

//默认方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

到这里,我们就应该明白了,其实就是高优先级的视图控制器要跟随低优先级控制器的旋转配置。这样就能够达到目的。

方法2: 另辟蹊径,使用模态视图

使用模态视图可以不受这种根视图控制器优先级的限制。这个也很容易理解,模态弹出的视图控制器是隔离出来的,不受根视图控制的影响。具体的设置和普通视图器代码相同,这里就不累述了。

六、实现需求:项目主要界面竖屏,部分界面横屏

这其实也是一个我们做屏幕旋转最常见的需求,在根据上面的讲述之后,我们实现这个需求会很容易,但是具体的实现却有着不同的思路,我在这里总结了两种方法:

方法1:使用基类控制器逐级控制

步骤:
1.开启全局权限设置项目支持的旋转方向
2.根据第五节中的方法1,自定义标签控制器和导航控制器来设置屏幕的自动旋转。
3.自定义基类控制器设置不支持自动转屏,并默认只支持竖屏
4.对项目中需要转屏幕的控制器开启自动转屏、设置支持的旋转方向并设置默认方向

demo1链接: https://github.com/DreamcoffeeZS/Demo_TestRotatesOne.git

方法2:Appdelegate增设旋转属性

步骤:
1.在Applegate文件中增加一个用于记录当前屏幕是否横屏的属性
2.需要横屏的界面,进入界面后强制横屏,离开界面时恢复竖屏

demo2链接: https://github.com/DreamcoffeeZS/Demo_TestRotatesTwo.git

七、默认横屏无效的问题

在上面的项目中,我们可能会遇到一个关于默认横屏的问题,把它拿出来细说一下。 我们项目中有支持竖屏的界面A,也有支持横竖屏的界面B,而且界面B需要进入时就显示横屏。从界面A到界面B中,如果我们使用第五节中的方法1会遇到无法显示默认横屏的情况,因为没有旋转设备,shouldAutorotate就没被调用,也就没法显示我们需要的横屏。这里有两个解决方法:

方法1:在自定义导航控制器中增加以下方法

#pragma mark -UINavigationControllerDelegate
//不要忘记设置delegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [self presentViewController:[UIViewController new] animated:NO completion:^{
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

这个方法的缺点是,原理上利用弹出模态视图来调用转屏,造成切换界面的时候有闪烁效果,体验不佳。所以这里也只是提供一种思路,不推荐使用。

方法2:在需要默认横屏的界面里设置,进入时强制横屏,离开时强制竖屏

关于这种使用,这个具体可以参考第五节中的demo2

注:两种方法不可同时使用

八、关于旋转后的适配问题

屏幕旋转的实现会带来相应的UI适配问题,我们需要针对不同方向下的界面重新调整视图布局。首先我们要能够监测到屏幕旋转事件,这里分为两种情况:

1.视图控制器UIViewController里的监测

当发生转屏事件的时候,下面的UIViewControoller方法会监测到视图View的大小变化,从而帮助我们适配

/*
This method is called when the view controller's view's size is
changed by its parent (i.e. for the root view controller when its window rotates or is resized).

If you override this method, you should either call super to
propagate the change to children or manually forward the 
change to children.
 */
- (void)viewWillTransitionToSize:(CGSize)size 
withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);

从注释里可以看出此方法在屏幕旋转的时候被调用,我们使用时候也应该首先调用super方法,具体代码使用示例如下:

//屏幕旋转之后,屏幕的宽高互换,我们借此判断重新布局
//横屏:size.width > size.height
//竖屏: size.width < size.height
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    if (size.width > size.height) {
        //横屏设置,为防止遮挡键盘,调整输入视图的高度
        self.textView_height.constant = 50;
    }else{
        //竖屏设置
        self.textView_height.constant = 200;
    }
}

2.子视图横竖屏监测

如果是类似于表视图的单元格,要监测到屏幕变化实现适配,我们需要用到layoutSubviews方法,因为屏幕切换横竖屏时会触发此方法,然后我们根据状态栏的位置就可以判断横竖屏了,代码示例如下:

- (void)layoutSubviews {
    [super layoutSubviews];
     //通过状态栏电池图标判断横竖屏
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationMaskPortrait) {
        //竖屏布局
    } else {
        //横屏布局
    }
}

九、APP启动即全屏

有时项目需要从App启动就默认是横屏,这里有个很方便的方法,就是我们在Device Orientation属性配置里设置如下:

image.png

但是只这样处理的话,会让项目只支持横屏,所以我们可以在Appdelegate里再次调整我们所支持的方向,方法已经说过,这里就不累述了。

最后总结:

关于屏幕旋转的使用大致总结到这里了,如果存在疏漏与错误欢迎路过的朋友指正!谢谢~

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.01.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • 一、最让人纠结的三种枚举
    • 1. 设备方向:UIDeviceOrientation
      • 2.页面方向:UIInterfaceOrientation
        • 3.页面方向:UIInterfaceOrientationMask
        • 二、两种屏幕旋转的触发方式
          • 情况1:系统没有关闭自动旋转屏幕功能,
            • 情况2:单个界面强制旋转
            • 三、屏幕旋转控制的优先级
            • 四、开启屏幕旋转的全局权限
              • 1.Device Orientation属性配置
                • 2.Info.Plist设置
                  • 3.Appdelegate&&Window中设置
                    • 五、开启屏幕旋转的局部权限(视图控制器)
                      • 方法1:逐级设置各视图控制器,高优先级的视图控制器影响低优先级控制器,
                        • 方法2: 另辟蹊径,使用模态视图
                        • 六、实现需求:项目主要界面竖屏,部分界面横屏
                          • 方法1:使用基类控制器逐级控制
                            • 方法2:Appdelegate增设旋转属性
                              • 方法1:在自定义导航控制器中增加以下方法
                                • 方法2:在需要默认横屏的界面里设置,进入时强制横屏,离开时强制竖屏
                                • 八、关于旋转后的适配问题
                                  • 1.视图控制器UIViewController里的监测
                                    • 2.子视图横竖屏监测
                                    • 九、APP启动即全屏
                                    • 最后总结:
                                    领券
                                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档