Swift 2: 屏幕旋转仅适用于全屏视频
Swift 2: screen rotation only on full screen video
这是一个热门问题,但我找不到任何适用于 Swift 2.
的解决方案
该应用仅支持纵向。但是在观看全屏视频(例如 YouTube)时,用户应该能够旋转到横向。
在Objective C上,这是最简单的解决方案,我用了很长时间:
AppDelegate file:
static NSString * const VIDEO_CONTROLLER_CLASS_NAME_IOS7 = @"MPInlineVideoFullscreenViewController";
static NSString * const VIDEO_CONTROLLER_CLASS_NAME_IOS8 = @"AVFullScreenViewController";
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if ([[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(VIDEO_CONTROLLER_CLASS_NAME_IOS7)] ||
[[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(VIDEO_CONTROLLER_CLASS_NAME_IOS8)]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
当视频全屏播放时,这允许所有方向。否则,仅纵向。
但我很难在 Swift 上完成这项工作。在 Swift 上播放全屏视频时是否可以使屏幕旋转?
这样的事情怎么样?
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
var classesToCheckFor = [AnyClass]()
if let ios7Class = NSClassFromString("MPInlineVideoFullscreenViewController") {
classesToCheckFor.append(ios7Class)
}
if let ios8Class = NSClassFromString("AVFullScreenViewController") {
classesToCheckFor.append(ios8Class)
}
for classToCheckFor in classesToCheckFor {
if (self.window?.rootViewController?.presentedViewController?.isKindOfClass(classToCheckFor) != nil) {
return .AllButUpsideDown
}
}
return .Portrait
}
NSClassFromString
可能 return nil
,但 isKindOfClass
需要一个非可选的 AnyClass
。我正在检查是否每个 class 都可以加载到平台上,将加载的 classes 添加到数组,然后遍历 classes 的数组,检查查看 presentedViewController
是否属于 class。如果是,我们 return .AllButUpsideDown
。如果 class 都无法加载,或者 presentedViewController
不是 class 中的任何一个,那么我们 return .Portrait
.
这里是 iOS 10 的解决方案:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let presentedViewController = window?.rootViewController?.presentedViewController {
let className = String(describing: type(of: presentedViewController))
if ["MPInlineVideoFullscreenViewController", "MPMoviePlayerViewController", "AVFullScreenViewController"].contains(className)
{
return UIInterfaceOrientationMask.allButUpsideDown
}
}
return UIInterfaceOrientationMask.portrait
}
我正在根据其他人的回答使用此代码
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let videoClass = NSClassFromString("AVFullScreenViewController"), self.window?.rootViewController?.presentedViewController?.isKind(of: videoClass) != nil {
return .allButUpsideDown
}
return [.portrait]
}
Swift Natividad Lara Diaz 2.2 版回答:
if let presentedViewController = window?.rootViewController?.presentedViewController {
let className = String(presentedViewController.dynamicType)
if ["MPInlineVideoFullscreenViewController", "MPMoviePlayerViewController", "AVFullScreenViewController"].contains(className) {
return UIInterfaceOrientationMask.All
}
}
我发现这个解决方案很容易工作 swift 3:
在AppDelegate.swift中添加这个函数:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if window == self.window {
return .portrait
} else {
return .allButUpsideDown
}
}
这是一个热门问题,但我找不到任何适用于 Swift 2.
的解决方案该应用仅支持纵向。但是在观看全屏视频(例如 YouTube)时,用户应该能够旋转到横向。
在Objective C上,这是最简单的解决方案,我用了很长时间:
AppDelegate file:
static NSString * const VIDEO_CONTROLLER_CLASS_NAME_IOS7 = @"MPInlineVideoFullscreenViewController";
static NSString * const VIDEO_CONTROLLER_CLASS_NAME_IOS8 = @"AVFullScreenViewController";
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if ([[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(VIDEO_CONTROLLER_CLASS_NAME_IOS7)] ||
[[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(VIDEO_CONTROLLER_CLASS_NAME_IOS8)]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
当视频全屏播放时,这允许所有方向。否则,仅纵向。
但我很难在 Swift 上完成这项工作。在 Swift 上播放全屏视频时是否可以使屏幕旋转?
这样的事情怎么样?
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
var classesToCheckFor = [AnyClass]()
if let ios7Class = NSClassFromString("MPInlineVideoFullscreenViewController") {
classesToCheckFor.append(ios7Class)
}
if let ios8Class = NSClassFromString("AVFullScreenViewController") {
classesToCheckFor.append(ios8Class)
}
for classToCheckFor in classesToCheckFor {
if (self.window?.rootViewController?.presentedViewController?.isKindOfClass(classToCheckFor) != nil) {
return .AllButUpsideDown
}
}
return .Portrait
}
NSClassFromString
可能 return nil
,但 isKindOfClass
需要一个非可选的 AnyClass
。我正在检查是否每个 class 都可以加载到平台上,将加载的 classes 添加到数组,然后遍历 classes 的数组,检查查看 presentedViewController
是否属于 class。如果是,我们 return .AllButUpsideDown
。如果 class 都无法加载,或者 presentedViewController
不是 class 中的任何一个,那么我们 return .Portrait
.
这里是 iOS 10 的解决方案:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let presentedViewController = window?.rootViewController?.presentedViewController {
let className = String(describing: type(of: presentedViewController))
if ["MPInlineVideoFullscreenViewController", "MPMoviePlayerViewController", "AVFullScreenViewController"].contains(className)
{
return UIInterfaceOrientationMask.allButUpsideDown
}
}
return UIInterfaceOrientationMask.portrait
}
我正在根据其他人的回答使用此代码
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let videoClass = NSClassFromString("AVFullScreenViewController"), self.window?.rootViewController?.presentedViewController?.isKind(of: videoClass) != nil {
return .allButUpsideDown
}
return [.portrait]
}
Swift Natividad Lara Diaz 2.2 版回答:
if let presentedViewController = window?.rootViewController?.presentedViewController {
let className = String(presentedViewController.dynamicType)
if ["MPInlineVideoFullscreenViewController", "MPMoviePlayerViewController", "AVFullScreenViewController"].contains(className) {
return UIInterfaceOrientationMask.All
}
}
我发现这个解决方案很容易工作 swift 3:
在AppDelegate.swift中添加这个函数:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if window == self.window {
return .portrait
} else {
return .allButUpsideDown
}
}