[英]Force orientation not working in iOS 10 devices
目前我正在按AVPlayer全屏按鈕時改變力的方向。
final class NewMoviePlayerViewController: AVPlayerViewController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if contentOverlayView?.bounds == UIScreen.main.bounds{
DispatchQueue.main.async {
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
print("full screen")
}else{
DispatchQueue.main.async {
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
print("half screen")
}
}
}
上面的代碼在iOS 11中正常工作,但相同的代碼在iOS 10及以下版本中不工作
我們終於通過以下方式修復了它...
我們在AppDelegate上采用了一個Bool
變量來檢測方向變化。
var isLandScapeManualCheck = Bool()
接下來,我們實現了以下應用程序委托
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if isLandScapeManualCheck == false{
return UIInterfaceOrientationMask.portrait
}else{
return UIInterfaceOrientationMask.landscapeRight
}
// return UIInterfaceOrientationMask.portrait
}
根據布爾值,我們返回了定向模式。
iOS 10及以下版本應遵循這種方式。
在播放器控制器視圖中。.(表示家庭控制器)
if #available(iOS 11, *) {
}else{
playerVwController.contentOverlayView!.addObserver(self, forKeyPath: "bounds", options: NSKeyValueObservingOptions.new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "bounds"{
let rect = change![.newKey] as! NSValue
if let playerRect: CGRect = rect.cgRectValue as CGRect {
if playerRect.size == UIScreen.main.bounds.size {
print("Player in full screen")
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
isLandScapeManualCheck = true
} else {
DispatchQueue.main.async {
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
print("Player not in full screen")
isLandScapeManualCheck = false
}
}
}
}
}
在iOS 11之后
您應該在viewDidLayoutSubviews
調用
UIViewController.attemptRotationToDeviceOrientation()
所以最后您的子類代碼如下
final class NewMoviePlayerViewController: AVPlayerViewController {
override func viewDidLayoutSubviews() {
UIViewController.attemptRotationToDeviceOrientation()
super.viewDidLayoutSubviews()
if contentOverlayView?.bounds == UIScreen.main.bounds{
DispatchQueue.main.async {
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
// self.contentOverlayView?.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
print("full screen")
}else{
DispatchQueue.main.async {
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
// self.contentOverlayView?.transform = CGAffineTransform.identity
print("half screen")
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.