使用swift 3在VLCPlayer中播放电影时完全旋转横向视图 [英] Rotate totally Landscape view when movie play in VLCPlayer with swift 3

查看:269
本文介绍了使用swift 3在VLCPlayer中播放电影时完全旋转横向视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在电影播放时旋转我的视图控制器。

我的问题可能是重复但我在堆栈溢出时发现了很多方法。但是所有代码都不起作用。可能是我把代码放错了。

I would like to rotate my view controller when movie play.
My question might be duplicate but I tried many ways when I find in stack overflow. But all codes do not work. May be I put the codes in wrong way.

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

I put this code to vlcplayerviewcontroller.swift file.

当用户点击播放按钮时,它将转到vlcplayerviewcontroller.swift,我想自动以横向模式显示电影。我不知道怎么能这样做。

我的参考链接是如何仅在Swift中将一个视图控制器的方向锁定为纵向模式

When user clicks on play button, then it will goes to vlcplayerviewcontroller.swift and I would like to show the movie in Landscape mode automatically. I have no idea of how can I do this.
My reference link is How to lock orientation of one view controller to portrait mode only in Swift.

推荐答案

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

Allow only Portrait Device Orientation under project setting. Add these lines into your AppDelegate file

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方法中添加此行

Add this line in viewDidLoad method of your view_controller_for_rotate

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

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

And add these methods in same class

    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
    }

我希望这有帮助。

这篇关于使用swift 3在VLCPlayer中播放电影时完全旋转横向视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆