快速旋转VLCPlayer中的电影时,完全旋转横向视图3

我想在电影播放时旋转我的视图控制器。
我的问题可能是重复的,但当我发现堆栈溢出时,我尝试了很多方法。 但是所有的代码都不起作用。 可能是我把代码以错误的方式。

override var shouldAutorotate: Bool { return false } override var supportedInterfaceOrientations: UIInterfaceOrientationMask { //return[.portrait,.landscapeRight] return .landscapeLeft } override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { //return UIInterfaceOrientation.landscapeRight return .landscapeLeft } 

我把这个代码到vlcplayerviewcontroller.swift文件。

在这里输入图像说明

当用户点击播放button时,它会去vlcplayerviewcontroller.swift,我想自动显示在横向模式的电影。 我不知道我该怎么做。
我的参考链接是如何将一个视图控制器的方向仅在Swift中locking到肖像模式 。

我在链接的答案有一个解决scheme,但我调整了景观。 按照这些步骤,它会给你所需的function。

Swift 3

在AppDelegate中:

 /// set orientations you want to be allowed in this property by default var orientationLock = UIInterfaceOrientationMask.all func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return self.orientationLock } 

在其他一些全局结构体或帮助器类中,我在这里创build了AppUtility:

 struct AppUtility { static func lockOrientation(_ orientation: UIInterfaceOrientationMask) { if let delegate = UIApplication.shared.delegate as? AppDelegate { delegate.orientationLock = orientation } } /// Added method to adjust lock and rotate to the desired orientation static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) { self.lockOrientation(orientation) UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation") } } 

然后在所需的ViewController中,您想要locking和旋转方向,如电影控制器:

  override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) // rotate and lock AppUtility.lockOrientation(.landscape, andRotateTo: .landscapeRight) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) // Don't forget to reset when view is being removed AppUtility.lockOrientation(.all) } 

在项目设置下仅允许纵向设备方向。 将这些行添加到您的AppDelegate文件中

 func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) { if (rootViewController.isKind(of: your_class_name_for_rotate.self)) { // Unlock landscape view orientations for this view controller return .allButUpsideDown; } } // Only allow portrait (standard behaviour) return .portrait; } private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? { if (rootViewController == nil) { return nil } if (rootViewController.isKind(of: UITabBarController.self)) { return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController) } else if (rootViewController.isKind(of: UINavigationController.self)) { return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController) } else if (rootViewController.presentedViewController != nil) { return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController) } return rootViewController } 

将这一行添加到view_controller_for_rotate的viewDidLoad方法中

 let value = UIInterfaceOrientation.landscapeLeft.rawValue UIDevice.current.setValue(value, forKey: "orientation") 

并在同一类中添加这些方法

  override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation") } private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.landscapeLeft } private func shouldAutorotate() -> Bool { return true } 

我希望这有帮助。