【问题标题】:Rotate totally Landscape view when movie play in VLCPlayer with swift 3使用swift 3在VLCPlayer中播放电影时完全旋转横向视图
【发布时间】:2017-09-03 15:51:49
【问题描述】:

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

 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 文件中。

当用户点击播放按钮时,它将转到 vlcplayerviewcontroller.swift,我想自动以横向模式显示电影。我不知道我该怎么做。
我的参考链接是How to lock orientation of one view controller to portrait mode only in Swift

【问题讨论】:

    标签: ios uiviewcontroller swift3 landscape uideviceorientation


    【解决方案1】:

    在项目设置下仅允许纵向设备方向。 将这些行添加到您的 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
        }
    

    我希望这会有所帮助。

    【讨论】:

    • 是的,兄弟。其实,我还有一趟要走的路。所以,我会在我测试后回复你。非常感谢兄弟。
    【解决方案2】:

    我在链接的答案中有一个解决方案,但我针对横向进行了调整。按照以下步骤操作,它会为您提供所需的功能。

    斯威夫特 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
    }
    

    在其他一些全局结构或辅助类中,我在这里创建了 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 中,例如您的电影控制器:

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

    【讨论】:

    • 兄弟@bmjohns,我在哪里将AppUtility代码放在应用程序中? Overlayviewcontroller.swift?
    • 你可以在任何地方添加它。但是我会把它放在一个新的 swift 文件中,这样你就可以在应用程序的任何地方访问代码,以防你想在其他时间旋转和锁定。
    • 兄弟@bmjohns,电影播放器​​不旋转:(
    • @MayPhyu 好的,你能提供更多信息吗?调试时会发生什么?您是否看到方法在您期望的时候被调用?您的 plist 是否支持所有旋转?
    • 兄弟@bmjohns,旋转问题已解决。但是我遇到了另一个问题stackoverflow.com/questions/43929485/…。你能看看这个吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多