繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM