这个标题有点绕,因此我来解释一下。git
以前项目开发的时候须要用到一个视频播放的第三方库,可是这个第三方库在从小屏幕播放切换到大屏幕播放的时候,是须要开启项目的自动旋转支持的。也就是 github
第三方视频播放库 BMPlayer 这个库中有许多细节的部分没法知足实际使用,所以我下了源码后本身根据需求修改。swift
然而实际应用中,许多的 App 的要求是须要禁止横屏的。由于大多数 App(窃觉得除了游戏之外的)即便写好了约束,也都不会针对横屏下的 UI 做对应的优化。 所以咱们须要本身针对工程禁用横屏状况下,播放视频时切换到全屏作本身的优化
app
以前搜索播放框架的时候没有找到适当的,所以最后决定本身把 BMPlayer
的源码 down
下来本身修改。虽然最后解决了(这也是本篇文章下面主要阐述的内容),可是解决方案是在AppDelegate
中增长一个变量来控制当前App
的方向。实际上仍是增长了库和工程之间的耦合度。暂时还未找到更好的解决方案。框架
Swift
为例)下面的源码中略去了没必要要的代码函数
let appDelegate = UIApplication.shared.delegate as! AppDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, WXApiDelegate {
var deviceOrientation = UIInterfaceOrientationMask.portrait
// 去实现这个回调代理
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return deviceOrientation
}
}
复制代码
点击全屏播放和恢复小屏幕时候两个按钮的对应方法测试
private func forceToPlayOnFullScreen() {
let currentOrientation = UIDevice.current.orientation
print("向右旋转前 当前的方向 = ", currentOrientation.rawValue)
appDelegate.deviceOrientation = .landscapeRight
// 若是防止用户在进入当前页面时手机横置,那么当前的 device orientation 是 landscapeRight
// 这时候,咱们再去设置 landscapeRight,是不会引发广播 "UIDeviceOrientationDidChangeNotification",所以手动调用一次
let portraitValue = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(portraitValue, forKey: "orientation")
let rightValue = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(rightValue, forKey: "orientation")
}
private func forceToPlayOnOriginScreen() {
let currentOrientation = UIDevice.current.orientation
print("恢复垂直前当前的方向 = ", currentOrientation.rawValue)
// 这里为何要写两遍,在下面说明
appDelegate.deviceOrientation = .portrait
let rightValue = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(rightValue, forKey: "orientation")
let portraitValue = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(portraitValue, forKey: "orientation")
}
复制代码
必定很奇怪,为何两次代码的调用中,设置方向的代码要写两遍呢优化
UIDevice.current.setValue
ui
这是由于,视频播放库,这里须要去获取一个系统的广播 UIDeviceOrientationDidChangeNotification
.spa
假设咱们当前的屏幕是横屏的状况
此时系统会利用陀螺仪,虽然咱们视觉上看上去,他并无向右旋转,可是当前的方向
UIDevice.current.orientation
会变成landScapeRight
.
所以咱们若是这时候去调用
let rightValue = UIInterfaceOrientation.landscapeRight.rawValue UIDevice.current.setValue(rightValue, forKey: "orientation") 复制代码
这个函数的时候,不会触发广播,由于系统会认为,其实个人方向没有变化啊!
这也就是为何上面咱们会先让他的方向设为默认的 portrait
再切回landscapeRight
. 我曾经误觉得界面会不会旋转回去一次再赚回来,可是实际测试发现却是没有出现相似的情景。