力方向iOS中不工作10设备
问题描述:
目前我在做力方向变化时AVPlayer全屏按钮pressed.I试过类似力方向iOS中不工作10设备
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及以下
版本工作
答
我们终于用下面的方式固定它...
我们已经采取了一个Bool
变量上APPD elegate检测方向变化。
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及以下版本应该遵循这样..
在您的PlayerController视图。(意思是你的家控制器)
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()
因此,最后你的子类代码像belo瓦特
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")
}
}
}
'contentOverlayView .bounds == UIScreen.main.bounds'为真在IOS 10(及以下)或问题是内部'UIDevice.current.setValue(值,forKey: “取向”)?' ? –
@AlbertoCantallops里面的条件,但强制方向代码不工作 – karthikeyan
看看https://*.com/questions/26357162/how-to-force-view-controller-orientation-in-ios-8有一堆不同的方法。注意接受的答案是你是如何做到的,但https://*.com/a/28220616/7064692也许可以帮助 –