【问题标题】:Swift 2: screen rotation only on full screen videoSwift 2:仅在全屏视频上进行屏幕旋转
【发布时间】:2016-02-12 02:13:40
【问题描述】:

这是一个热门问题,但我找不到任何适用于 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 上播放全屏视频时是否可以让屏幕旋转?

【问题讨论】:

    标签: ios swift swift2 screen-orientation


    【解决方案1】:

    这样的事情呢?

    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 上运行良好。谢谢!
    • 为确保应用返回纵向,最好使用“supportedInterfaceOrientations”而不是“supportedInterfaceOrientationsForWindow”。这样,当视频关闭时,方向会自动恢复为纵向。
    • 谢谢,这是匆忙和使用自动完成的副产品...很高兴你让它工作了。
    【解决方案2】:

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

      【讨论】:

        【解决方案4】:

        我正在根据其他人的回答使用此代码

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

        【讨论】:

          【解决方案5】:

          我发现这个解决方案很容易工作,无需任何 swift 3 的努力:

          在 AppDelegate.swift 中,添加这个函数:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-04-26
            • 2014-07-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多