gpt4 book ai didi

ios - 使用 swift 3 在 VLCPlayer 中播放电影时完全旋转横向 View

转载 作者:行者123 更新时间:2023-11-28 21:10:56 25 4
gpt4 key购买 nike

我想在电影播放时旋转我的 View Controller 。
我的问题可能是重复的,但是当我发现堆栈溢出时,我尝试了很多方法。但并非所有代码都有效。可能是我把代码放错了。

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

enter image description here

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

最佳答案

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

希望对您有所帮助。

关于ios - 使用 swift 3 在 VLCPlayer 中播放电影时完全旋转横向 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43292059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com