Swift 2:仅在全屏视频上旋转屏幕

时间:2016-02-12 02:13:40

标签: ios swift swift2 screen-orientation

这是一个很受欢迎的问题,但我找不到任何适用于Swift 2的解决方案。

该应用仅限人像。但是,在观看YouTube等全屏视频时,用户应该可以旋转到横向。

在目标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 个答案:

答案 0 :(得分:6)

这样的事情怎么样?

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

答案 1 :(得分: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

}

答案 2 :(得分:1)

Swift 2.2版本的Natividad Lara Diaz回答:

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

答案 3 :(得分: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]
  }

答案 4 :(得分:0)

我发现这个解决方案非常容易,没有为swift 3做任何努力:

在AppDelegate.swift中

,添加此功能:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if window == self.window {
        return .portrait
    } else {
        return .allButUpsideDown
    }
}