美文网首页iOS开发
iOS 屏幕旋转的实践解析

iOS 屏幕旋转的实践解析

作者: 下班不写程序 | 来源:发表于2023-02-12 17:01 被阅读0次

本篇主要通过四个方面来解析屏幕旋转:
1、实现旋转的方式之跟随手机感应旋转
2、实现旋转的方式之手动旋转
3、屏幕旋转的场景应用
4、易混淆的枚举值
下面来逐条分析:

一、跟随手机感应器旋转

1. 两种方法可以设置屏幕旋转的全局权限:

1.1 Device Orientation 属性配置:

新建项目 -> TARGET -> General -> Deployment Info


Device Orientation
  • iPhone 默认竖屏展示, 当系统屏幕旋转开关未锁定时, 就可以自由的转动, 值得注意的是iPhone 不支持旋转到 Upside Down 方向
  • iPad 默认竖屏展示, 当系统屏幕旋转开关未锁定时, 伴随iPad方向展示, 就可以自由的转动。
系统屏幕旋转开关
1.2 实现Appdelegate 的supportedInterfaceOrientationsForWindow 方法:
// 返回需要支持的方向
// 如果我们实现了Appdelegate的这一方法,那么我们的App的全局旋转设置将以这里的为准
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskAll;
}

总结: 以上两种方式优先级:Appdelegate方法 > Target 配置

2. 单一控制器处理屏幕跟随手机感应器旋转

只要让当前的视图控制器实现以下三个方法:

/** 是否自动旋转
 - iOS16.0已过期, 不会再调用.
 - iOS16.0 之前, 先调用supportedInterfaceOrientations 确定支持的屏幕方向, 再调用shouldAutorotate 是否自动旋转, 来确定控制器方向.
 - iOS16.0 之前, preferredInterfaceOrientationForPresentation 并未调用.
 - iOS16.0 之前, return NO 之后, 当前控制器都支持屏幕旋转了
 */
- (BOOL)shouldAutorotate {
    return YES;
}

/// 当前控制器支持的屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

/// 优先的屏幕方向 - 只会在 presentViewController:animated:completion时被调用
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

这种方法需要注意以下几点:

  • shouldAutorotate返回 YES 表示跟随系统旋转,但是受 supportedInterfaceOrientations 方法的返回值影响,只支持跟随手机传感器旋转到支持的方向.
  • preferredInterfaceOrientationForPresentation 需要返回 supportedInterfaceOrientations 中支持的方向,不然会发生'UIApplicationInvalidInterfaceOrientation'崩溃.
  • iOS16.0 之后单纯只会调用supportedInterfaceOrientations, shouldAutorotate就不会被调用了。

3. 既配置了屏幕旋转的全局权限, 也配置了单一控制器屏幕旋转权限

1.11.2两种方式的配置和控制器的 supportedInterfaceOrientations 方法(2)都会影响最终视图控制器最终支持的方向。

iOS 16 之前present打开控制器的方式为例,当前控制器最终支持的屏幕方向,取决于上面两种方式中的优先级最高的方式的值,与控制器 supportedInterfaceOrientations 的交集。

总结起来有以下几种情况:

  • 如果交集为空,且在控制器的 shouldAutorotate 方法中返回为YES,则会发生 UIApplicationInvalidInterfaceOrientation的崩溃。
  • 如果交集为空,且在控制器的 shouldAutorotate 方法中返回为NO,控制器的 supportedInterfaceOrientations 方法与 preferredInterfaceOrientationForPresentation方法返回值不冲突(前者返回值包含有后者返回值),则显示为控制器配置的方向。
  • 如果交集为空,且在控制器的 shouldAutorotate 方法中返回为 NO,控制器的 supportedInterfaceOrientations方法与 preferredInterfaceOrientationForPresentation方法返回值冲突(前者返回值未包含有后者返回值),则会发生UIApplicationInvalidInterfaceOrientation 的崩溃。
  • 如果交集不为空,控制器的 supportedInterfaceOrientations 方法与preferredInterfaceOrientationForPresentation 方法返回值冲突,则会发生 UIApplicationInvalidInterfaceOrientation 的崩溃。
  • 如果交集不为空,控制器的 supportedInterfaceOrientations 方法与 preferredInterfaceOrientationForPresentation 方法返回值不冲突,当前控制器则根据 shouldAutorotate 返回值决定是否在交集的方向内自动旋转。

所以, 这里建议如果没有全局配置的需求,就不要变更 Target 属性配置或实现 Appdelegate 方法,只需在要实现旋转效果的 ViewController 中按前面所说的方式去实现代码

而在 iOS 16之后, 所有崩溃的问题都被优化了, 同样, 当前控制器最终支持的屏幕方向,取决于上面两种方式中的优先级最高的方式的值,与控制器 supportedInterfaceOrientations 的交集。

总结起来有以下几种情况:

  • 如果交集为空,以控制器supportedInterfaceOrientations方法返回值为准
  • 如果交集不为空,且控制器supportedInterfaceOrientations 方法与preferredInterfaceOrientationForPresentation方法返回值不冲突,则以交集为准

二、手动旋转屏幕

这种方式在很多视频软件中都很常见,点击按钮后旋转至横屏。

在iOS 16 之前需要在 shouldAutorotate 中返回 yes,然后再在此方法中 UIInterfaceOrientation 传入你需要旋转到的方向。

- (void)changeVCToOrientation:(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];
    }
}

在iOS 16之后你会发现, 它已经失效了, fix如下:
https://www.jianshu.com/p/c11490a3668c

三、屏幕旋转的场景应用

  • 自动旋转
    如果你的 iPhone 没有关闭系统屏幕旋转,你就能发现系统相册 APP 的页面是可以跟着手机转动方向旋转的。

如果你想实现和它一样的效果,只需要按照前面方式一(跟随手机感应器旋转)去配置你的视图控制器的方法,之后控制器就可以在 supportedInterfaceOrientations返回的方向内实现自由旋转了。

  • 只能手动旋转
    这种场景比较少见,在视频直播类 APP 中常见的场景是自动和手动旋转相结合的方式。
    如果你要实现只能通过像点击按钮去旋转的方式,首先需要在 supportedInterfaceOrientations 方法中返回你需要支持的方向,这里重点是 shouldAutorotate 方法的返回值。

上面方式二中(手动旋转)说明了手动旋转需要 shouldAutorotate 返回 YES,但是这也会让控制器支持自动旋转,不符合这个需求,所以我们按以下方法处理:

- (BOOL)shouldAutorotate {
    if (self.isRotationNeeded) {
        return YES;
    } else {
        return NO;
    }
}    

属性 isRotationNeeded作为是否需要旋转的标记,isRotationNeeded默认为 NO,此时就算你旋转设备,回调 shouldAutorotate 方法时也不会返回 YES,所以屏幕也不会自动旋转。
剩下的只需要你在点击旋转的按钮后将isRotationNeeded 置为YES 并调用手动旋转的方法,这样处理后只能手动旋转的效果就实现了。

4、相关枚举值

4.1 设备方向:UIDeviceOrientation

UIDeviceOrientation 是以home 键的位置作为参照,枚举值指的是设备倒向的方向, 受传感器影响,和当前屏幕显示的方向无关,所以只能取值不能设值
前面讲述的屏幕旋转方法中不会直接用到这个枚举,但是如果你有监听设备当前方向的需求时,它就变得有用了。可以通过 [UIDevice currentDevice].orientation 获取当前设备的方向,若要监听设备的方向变化,可以用以下代码实现:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
 [[NSNotificationCenter defaultCenter] addObserver:observer
                                          selector:@selector(onDeviceOrientationChange:)
                                              name:UIDeviceOrientationDidChangeNotification
                                            object:nil];
- (void)onDeviceOrientationChange:(NSNotification *)noti {
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    switch (deviceOrientation) {
        case UIDeviceOrientationPortrait:
            NSLog(@"竖屏");
            break;
        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"横屏(Home在右边,设备向左边倒)");
            break;
        case UIDeviceOrientationLandscapeRight:
            NSLog(@"横屏(Home在左边,设备向右边倒)");
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"Home在上边");
            break;
        case UIDeviceOrientationFaceUp:
            NSLog(@"屏幕向上");
            break;
        case UIDeviceOrientationFaceDown:
            NSLog(@"屏幕向下");
            break;
        default:
            break;
    }
}

4.2 页面方向:UIInterfaceOrientation

UIInterfaceOrientation 是当前视图控制器的方向,区别于设备方向,它是屏幕正在显示的方向, 也就是当前页面home所处的方向

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} API_UNAVAILABLE(tvos);

4.3 页面方向:UIInterfaceOrientationMask

观察 UIInterfaceOrientationMask 枚举的值,我们就会发现这是一种为了支持多种 UIInterfaceOrientation 而定义的类型,它用来作为 supportedInterfaceOrientations 方法的返回值,比如我们在该方法中返回 UIInterfaceOrientationMaskAll 就可以支持所有方向了。

/// 当前 VC支持的屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}
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),
} API_UNAVAILABLE(tvos);

结语

路漫漫其修远兮,吾将上下而求索~

作者简书

作者掘金

作者GitHub

.End

相关文章

网友评论

    本文标题:iOS 屏幕旋转的实践解析

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