正文
我们公司的 app 只支持竖屏, 只有在视频播放的时候才可以横屏, 所以这就需要我们强制去旋转屏幕. 我想一般的 app 大概都会有这种需求.
最近随着 ios16 的更新, 线上的 app 在 ios16 系统上不管用了, 原因就是苹果从 ios16 开始, 更改了屏幕旋转的机制, 以后都要用 uiwindowscence 这个 api 类. 所以我们的 app 就只能根据版本去做适配, 新的要支持, 老的也要兼容.
在这里, 我就直接上干货, 只展示重要代码, 就不写 demo, 没什么技术含量, 做为一个日常记录分享而已.
重点提示
xcode 14.0macos 12.5手机 ios15.1 和 ios16
一. appdelegate 配置
定义一个 bool 类型的变量
全局控制否是横屏代理方法根据这个变量来返回是 竖屏 还是 横屏, ios16 及以上可以做到根据屏幕方向适配横屏, 我们公司要求不高, 所以我们是强制右横屏, 这一点是不太友好, 这不是重点.
- 这一步
swift和objc没什么区别, 只是语法不同, 所以就只提供了swift代码.
@main
class appdelegate: uiresponder, uiapplicationdelegate {
var window: uiwindow?
// 定义一个 bool 类型的变量
var isfullscreen: bool = false
func application(_ application: uiapplication, supportedinterfaceorientationsfor window: uiwindow?) -> uiinterfaceorientationmask {
if isfullscreen {
if #available(ios 16.0, *) {
// 16 及以上可以做到根据屏幕方向适配横屏
return .landscape
} else {
// 16 以下不方便做, 所以我们是强制 右横屏
return .landscaperight
}
}
return .portrait
}
}
二. 适配 ios16 旋转屏幕
在原来基础上添加适配 ios16 的代码 在 vc 中点击横屏按钮时进行强制屏幕旋转, 这里强调一下, 播放器的横屏按钮操作最好是回调到当前 vc 中去操作, setneedsupdateofsupportedinterfaceorientations() 这个方法是 vc 的对象方法, 这里同样swift 和 objc 没什么区别, 只是语法不同.
func switchorientation(isfullscreen: bool) {
let kappdelegate = uiapplication.shared.delegate as? appdelegate
kappdelegate?.isfullscreen = isfullscreen
// 设置屏幕为横屏
if #available(ios 16.0, *) {
setneedsupdateofsupportedinterfaceorientations()
guard let scence = uiapplication.shared.connectedscenes.first as? uiwindowscene else {
return
}
let orientation: uiinterfaceorientationmask = isfullscreen ? .landscape : .portrait
let geometrypreferencesios = uiwindowscene.geometrypreferences.ios(interfaceorientations: orientation)
scence.requestgeometryupdate(geometrypreferencesios) { error in
print("强制\(isfullscreen ? "横屏" : "竖屏" )错误: \(error)")
}
} else {
let oriention: uideviceorientation = isfullscreen ? .landscaperight : .portrait
uidevice.current.setvalue(oriention.rawvalue, forkey: "orientation")
uiviewcontroller.attemptrotationtodeviceorientation()
}
// 更新 横竖屏对应的 ui
// ...
}
三. 强制旋转屏幕
在播放器横竖屏切换按钮的回调方法中调用 旋转屏幕方法即可, 不管手机有没有打开自动旋转, 都可以实现屏幕方向切换.
// 播放器 - 全屏按钮切换回调
func playerviewrotatescreen(isfull: bool) {
switchorientation(isfullscreen: isfull)
}
四. 自动旋转
手机需要打开自动旋转开关, 注册屏幕旋转通知, 监听屏幕旋转时的方向. 方法不只一种, 但是我就用下面这个.
- 一定要注意下面这两个方法, 否则有可能通知不生效, 一个开启一个关闭.
- uidevice.current.begingeneratingdeviceorientationnotifications()
- uidevice.current.endgeneratingdeviceorientationnotifications()
注意:我这里做的是 16 以下只支持右横屏, 16 不需要获取设备方向, 因此可以支持左/右横屏. 这也是appdelegate中区分版本的原因.
友情提示 :
最好是把侧滑返回手势给禁掉. 否则横屏侧滑返回就出问题了, 当然也可以做的更精细些, 横屏时禁止. 我做验证就简单些.
override func viewdidappear(_ animated: bool) {
super.viewdidappear(animated)
uidevice.current.begingeneratingdeviceorientationnotifications()
notificationcenter.default.addobserver(self, selector: #selector(screenchangedorientation(_:)), name: uidevice.orientationdidchangenotification, object: nil)
navigationcontroller?.interactivepopgesturerecognizer?.isenabled = false
}
override func viewwilldisappear(_ animated: bool) {
super.viewwilldisappear(animated)
navigationcontroller?.interactivepopgesturerecognizer?.isenabled = true
}
override func viewdiddisappear(_ animated: bool) {
super.viewdiddisappear(animated)
notificationcenter.default.removeobserver(self)
uidevice.current.endgeneratingdeviceorientationnotifications()
}
// 横竖屏监听
@objc private func screenchangedorientation(_ notification: notification) {
let info = notification.userinfo
guard let animated = info?["uideviceorientationrotateanimateduserinfokey"] as? int, animated == 1 else {
return
}
let orientation = uidevice.current.orientation
if orientation == uideviceorientation.landscapeleft || orientation == uideviceorientation.landscaperight {
// 横屏
videoview.changescreenorientation(isfull: true)
} else if orientation == uideviceorientation.portrait {
// 竖屏
videoview.changescreenorientation(isfull: false)
}
}以上就是配置ios 16 屏幕旋转适配实例详解的详细内容,更多关于ios 16 屏幕旋转适配的资料请关注代码网其它相关文章!
发表评论