强制方向在iOS 10设备中不起作用

时间:2017-10-09 11:50:57

标签: ios swift avplayerviewcontroller

目前我在按下 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及以下版本中无效

1 个答案:

答案 0 :(得分:4)

我们终于通过以下方式修复了它......

我们在 AppDelegate 上采用了一个var isLandScapeManualCheck = Bool() 变量来检测方向更改。

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

        if isLandScapeManualCheck == false{
        return UIInterfaceOrientationMask.portrait
        }else{

            return UIInterfaceOrientationMask.landscapeRight
        }
//        return UIInterfaceOrientationMask.portrait

    }

接下来,我们实施了以下应用委托

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
                    }

                }
            }
        }
    }

根据bool值,我们退回方向模式。

iOS 10及以下版本应遵循这种方式..

在你的播放器控制器视图..(意思是你的家庭控制器)

viewDidLayoutSubviews

iOS 11之后

您应该拨打 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")
        }

    }

}

最后你的子类代码如下

value