Swift 2: 仅针对全屏视频旋转屏幕

7

这是一个常见问题,但我无法找到适用于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上播放全屏视频时旋转屏幕?

5个回答

7
这个怎么样?
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 可能会返回 nil,但是 isKindOfClass 要求一个非空的 AnyClass 类。我在检查每个类是否能够加载到平台上,将已经加载的类添加到一个数组中,然后遍历这个类数组,检查 presentedViewController 是否属于这两个类中的任意一个。如果是,则返回 .AllButUpsideDown 。如果这两个类都无法加载或者 presentedViewController 不属于这两个类中的任意一个,则返回 .Portrait


iOS 8在旋转方面存在问题,但在iOS 9上运行良好。谢谢! - tomDev
为确保应用程序返回到纵向模式,最好使用“supportedInterfaceOrientations”而不是“supportedInterfaceOrientationsForWindow”。这样,当视频被关闭时,方向会自动返回到纵向模式。 - tomDev
谢谢,那是匆忙和使用自动完成的副产品...很高兴你把它搞定了。 - JAL

3

以下是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

}


1

Natividad Lara Diaz的答案的Swift 2.2版本:

if let presentedViewController = window?.rootViewController?.presentedViewController {
    let className = String(presentedViewController.dynamicType)
    if ["MPInlineVideoFullscreenViewController", "MPMoviePlayerViewController", "AVFullScreenViewController"].contains(className) {
       return UIInterfaceOrientationMask.All
   }
}

0
我发现这个解决方案非常容易,而且不需要任何努力就可以在Swift 3中运行:
在AppDelegate.swift中添加此函数:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if window == self.window {
        return .portrait
    } else {
        return .allButUpsideDown
    }
}

0

我正在使用基于他人答案的这段代码

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接