【问题标题】:Force orientation not working in iOS 10 devices强制方向在 iOS 10 设备中不起作用
【发布时间】:2017-10-09 11:50:56
【问题描述】:

目前,当按下 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 及以下版本中无法运行

【问题讨论】:

  • contentOverlayView?.bounds == UIScreen.main.bounds 在 iOS 10(及更低版本)中为真,或者问题在 UIDevice.current.setValue(value, forKey: "orientation") 内部?
  • @AlbertoCantallops 条件进入但强制方向代码不起作用
  • 看看stackoverflow.com/questions/26357162/… 有很多不同的方法。请注意,接受的答案是您如何做到的,但stackoverflow.com/a/28220616/7064692 可能会有所帮助
  • 感谢回复,已完成

标签: ios swift avplayerviewcontroller


【解决方案1】:

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

我们在 AppDelegate 上采用了一个 Bool 变量来检测方向变化。

var isLandScapeManualCheck = Bool()

接下来我们实现了以下应用delegate

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

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

            return UIInterfaceOrientationMask.landscapeRight
        }
//        return UIInterfaceOrientationMask.portrait

    }

基于 bool 值,我们返回 orientation 模式。

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()

所以最后你的子类代码如下所示

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")
        }

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多