首页 文章

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 回答

  • 0

    这样的事情怎么样?

    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 .

  • 6

    这里是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
    

    }

  • 3

    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
       }
    }
    
  • 1

    我正在使用此代码基于其他人的答案

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

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

    在AppDelegate.swift中,添加此函数:

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

相关问题