关于iOS16 以后屏幕旋转不得不说的事

news/2024/7/20 16:24:50 标签: ios, swift, 开发语言

由于笔者最近刚完成了一个强制屏幕横屏的需求 所以 顺便记录一下 实现过程中遇到 block点 也为广大的iOS开发者做一个笔记:

废话不多说直接上代码

swift"> //适配iOS16.0的横竖屏方法
    public static func switchInterfaceWtihIos16(orientation: UIInterfaceOrientation , orientationMask: UIInterfaceOrientationMask) {
        if #available(iOS 16.0, *) {
            let array = UIApplication.shared.connectedScenes
            let geometryPreferencesIOS = UIWindowScene.GeometryPreferences.iOS(interfaceOrientations: orientationMask)
            if  let scene = array.first as? UIWindowScene {
                UIViewController.attemptRotationToDeviceOrientation()
                SystemManager.currentViewController()?.setNeedsUpdateOfSupportedInterfaceOrientations()
                scene.requestGeometryUpdate(geometryPreferencesIOS) { e in
                    log.info("iOS16 屏幕旋转失败:error \(e)")
                    scene.requestGeometryUpdate(geometryPreferencesIOS)
                }
            }

        } else {
            let resetOrientationTarget = NSNumber(value: UIInterfaceOrientation.unknown.rawValue)
            UIDevice.current.setValue(resetOrientationTarget, forKey: "orientation")
            let orientationTarget = NSNumber(value: orientation.rawValue)
            UIDevice.current.setValue(orientationTarget, forKey: "orientation")
        }
    }

值得一提的是 iOS16以后新增了GeometryPreferences 方法来旋转屏幕,所以设置横竖屏需要 使用的新的方法,并且入参为 UIInterfaceOrientationMask

调用上述方法以后 还要在需要实现横屏controller重写下面的方法和属性

    // 是否支持旋转
    open override var shouldAutorotate: Bool {
        return false
    }
    
    // 支持的方向
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeRight
    }
    // 首次展示的方向
    open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return  .landscapeRight
    }
    @available(iOS 16.0, *)
    public override func setNeedsUpdateOfSupportedInterfaceOrientations() {
        let orientation = UIDevice.current.orientation
        if orientation == .landscapeLeft || orientation == .landscapeRight || orientation == .faceUp || orientation == .faceDown || isLandScreen {
            player.setfullScreen(isFull: true)
        } else {
            player.setfullScreen(isFull: false)
        }
    }

islandscreen 是我用来判断是否横屏的一个bool,直接复制可能会报错。大家复制后删除该判断再根据实际情况去修改就好

接下来着重说明一下 setNeedsUpdateOfSupportedInterfaceOrientations ,
这个是iOS16以后 的一个新方法,它的作用就是 每当屏幕旋转就会进入这个回调方法。当然你也可以主动调用,正如开篇方法中SystemManager.currentViewController()?.setNeedsUpdateOfSupportedInterfaceOrientations()
它的作用就是主动进入该方法中 。

然后通过查阅其他博主文章,如果想要屏幕旋转正常还需要加上UIViewController.attemptRotationToDeviceOrientation()
下面是官方描述 :

Summary

Attempts to rotate all windows to the orientation of the device.

所以老铁加上没毛病
这里总结一下

  1. 新建一个工具类或者拓展,加入代码块一 内容
  2. 在需要横竖屏的controller,复制 代码块二 内容
  3. 然后如果你的项目之前还没有做过横竖屏的适配你还得请添加图片描述
    做截图相关配置。

http://www.niftyadmin.cn/n/452333.html

相关文章

超好用的特性:Stream 流

一、介绍 JDK 8引入了Stream API,它是Java中处理集合数据的一种新方式。Stream流提供了一种更简洁、更灵活的方式来对集合进行操作和处理。 Stream流可以看作是一种高级的迭代器,它可以对集合中的元素进行过滤、映射、排序、聚合等操作,而不…

UE特效案例 —— 寒冰武器

一,环境配置 创建默认地形Landscape,如给地形上材质需确定比例;添加环境主光源DirectionalLight,设置相应的强度和颜色;PostProcessVolume设置曝光,设置Min/Max Brightness为1; 与关闭Game Sett…

VUE 2X MVVM模型 ③

目录 文章有误请指正,如果觉得对你有用,请点三连一波,蟹蟹支持✨ V u e j s Vuejs Vuejs M V V M MVVM MVVM模型Data与El的2种写法总结 文章有误请指正,如果觉得对你有用,请点三连一波,蟹蟹支持✨ ⡖⠒⠒⠒…

我们如何实现业务操作日志功能?

1. 需求 我们经常会有这样的需求,需要对关键的业务功能做操作日志记录,也就是用户在指定的时间操作了哪个功能,操作前后的数据记录,必要的时候可以一键回退,今天我就为大家实现这个的功能,让大家可以直接拿…

【C语言】项目实战——快速0基础上手五子棋游戏(内附源码)

君兮_的个人主页 勤时当勉励 岁月不待人 C/C 游戏开发 如果你是从现在关注的老粉的话,你可能会有点疑惑“how old are you?”(怎么老是你?) 唉,没办法我也不想的,但是月末了参加新星计划和2023年博客之星的评选只能更…

ubuntu设置开机启动命令

文章目录 概述系统版本设置开机启动命令1. 查看rc-local服务状态2. 设置rc-local服务开机启动3. 手动创建系统自启动服务4. 创建rc.local文件5. 设置rc.local文件权限6.添加开机启动命令7.启用rc-local服务8.查看rc-local服务状态9.重启系统 概述 本文档主要记录Ubuntu系统使用…

【linux kernel】linux内核数据结构分析之哈希表

👀Linux内核中实现了一套经典的哈希表操作,定义在/include/linux/list.h文件中,本文基于linux内核源码6.2.7,记录了其常用操作哈希表的API函数,便于在阅读linux内核源码时更好的理解程序执行的过程和细节。 &#x1f4…

C#提升(一、泛型)

一、什么是泛型 泛型,即“参数化类型” 我们来看以下代码,目的很明确,就是显示参数类型,这种类似的代码或者说只有参数类型不同,但是功能相同时,我们如何让代码写的更优雅? 在泛型没有出现的…