2016-02-12 9 views
7

上の画面の回転はこれが人気の質問ですが、私はアプリは唯一の肖像画であるスウィフト2.スウィフト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のプレーヤーのときに画面を回転させることはできますか?

答えて

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を返します。ここで

+0

のiOS 8が回転しますが、iOSの9のおかげで素晴らしい作品ではありません! – tomDev

+0

アプリがポートレートに戻ることを確認するには、「supportedInterfaceOrientationsForWindow」ではなく「supportedInterfaceOrientations」を使用する方がよいでしょう。このようにしてビデオが解除されると、向きは自動的にポートレートに戻ります。 – tomDev

+0

ありがとう、それは急いで、オートコンプリートを使用しての副産物だった...あなたはそれが働いてうれしい。 – JAL

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 

}

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

スウィフト2.2バージョンに答える:

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

私は、このソリューションは、迅速な3用の任意の努力なしに非常に簡単に動作することがわかった:

AppDelegate.swiftで、この機能を追加します。

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