我有一个包含AVPlayerViewController和AVPlayer的UIViewcontroller。 我想在AVPlayerViewController(视频全屏时)中启用旋转并禁用UIViewController的任何旋转。 如何仅在应用程序中的视频(全屏时)中启用旋转?
我有一个包含AVPlayerViewController和AVPlayer的UIViewcontroller。 我想在AVPlayerViewController(视频全屏时)中启用旋转并禁用UIViewController的任何旋转。 如何仅在应用程序中的视频(全屏时)中启用旋转?
Swift 2.2 在AppDelegate中允许播放器旋转:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
guard let vc = (window?.rootViewController?.presentedViewController) else {
return .Portrait
}
if (vc.isKindOfClass(NSClassFromString("AVFullScreenViewController")!)) {
return .AllButUpsideDown
}
return .Portrait
}
创建并使用AVPlayerViewController的子类,在播放器退出全屏模式时返回至竖屏模式:
class YourVideoPlayer: AVPlayerViewController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if view.bounds == contentOverlayView?.bounds {
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
}
}
对我来说,这样会更好。只需扩展AVPlayerViewController:
import AVKit
import UIKit
class AVPlayerViewControllerRotatable: AVPlayerViewController {
override var shouldAutorotate: Bool {
return true
}
}
Swift 4
supportedInterfaceOrientations
? - matt