0

ムービー再生時にビューコントローラーを回転させたいとします。
私の質問は重複しているかもしれませんが、私はスタックオーバーフローで見つけたときに多くの方法を試みました。しかし、すべてのコードは機能しません。私は間違った方法でコードを置くことがあります。スイフト3でVLCPlayerでムービーを再生すると完全に回転します。

override var shouldAutorotate: Bool { 
    return false 
} 

override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 
     //return[.portrait,.landscapeRight] 
    return .landscapeLeft 
} 
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { 
    //return UIInterfaceOrientation.landscapeRight 
    return .landscapeLeft 
} 

このコードをvlcplayerviewcontroller.swiftファイルに挿入しました。

enter image description here

ユーザーが再生ボタンをクリックすると、それはvlcplayerviewcontroller.swiftすると、私は自動的にランドスケープモードでは、動画を表示したいと思い行きます。私はこれをどうやってできるのか分かりません。
私の参照リンクはHow to lock orientation of one view controller to portrait mode only in Swiftです。

答えて

1

私はリンクされた答えの解決策がありましたが、私はランドスケープに調整しました。これらの手順を実行すると、目的の機能が得られます。

AppDelegateでスウィフト3

:いくつかの他のグローバル構造体やヘルパークラスで

/// set orientations you want to be allowed in this property by default 
var orientationLock = UIInterfaceOrientationMask.all 

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 
     return self.orientationLock 
} 

、ここで私はAppUtilityを作成:

したい希望のViewControllerで次に
struct AppUtility { 

    static func lockOrientation(_ orientation: UIInterfaceOrientationMask) { 

     if let delegate = UIApplication.shared.delegate as? AppDelegate { 
      delegate.orientationLock = orientation 
     } 
    } 

    /// Added method to adjust lock and rotate to the desired orientation 
    static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) { 

     self.lockOrientation(orientation) 

     UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation") 
    } 

} 

ムービーコントローラのように向きを固定したり回転させたりするには:

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    // rotate and lock 
    AppUtility.lockOrientation(.landscape, andRotateTo: .landscapeRight) 

} 

override func viewWillDisappear(_ animated: Bool) { 
    super.viewWillDisappear(animated) 

    // Don't forget to reset when view is being removed 
    AppUtility.lockOrientation(.all) 
} 
+0

Bro @ bmjohns、AppUtilityコードはどこに配置しますか? Overlayviewcontroller.swift? –

+0

どこにでも追加できます。しかし、私はそれを新しい素早いファイルに入れてしまい、他の時間を回転してロックしたい場合に備えて、アプリケーションのどこにでもコードにアクセスすることができます。 – bmjohns

+0

Bro @ bmjohns、ムービープレーヤーが回転しない:( –

1

プロジェクト設定でポートレートデバイス方向のみを許可します。

あなたのAppDelegateファイルにこれらの行を追加します
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 
    if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) { 
     if (rootViewController.isKind(of: your_class_name_for_rotate.self)) { 
      // Unlock landscape view orientations for this view controller 
      return .allButUpsideDown; 
     } 
    } 

    // Only allow portrait (standard behaviour) 
    return .portrait; 
} 

private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? { 
    if (rootViewController == nil) { return nil } 
    if (rootViewController.isKind(of: UITabBarController.self)) { 
     return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController) 
    } else if (rootViewController.isKind(of: UINavigationController.self)) { 
     return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController) 
    } else if (rootViewController.presentedViewController != nil) { 
     return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController) 
    } 
    return rootViewController 
} 

あなたview_controller_for_rotateのviewDidLoadメソッド

let value = UIInterfaceOrientation.landscapeLeft.rawValue 
    UIDevice.current.setValue(value, forKey: "orientation") 

にこの行を追加します。そして、私はこれが役に立てば幸い同じクラスに

override func viewWillDisappear(_ animated: Bool) { 
     super.viewWillDisappear(animated) 
     UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation") 
    } 
private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { 
     return UIInterfaceOrientationMask.landscapeLeft 
    } 
    private func shouldAutorotate() -> Bool { 
     return true 
    } 

をこれらのメソッドを追加します。

+0

ありがとうございます。あなたのコードを試してみます –

+0

@MayPhyu、投票がうまくいって答えを受け入れるなら、 –

+0

はい。実際、私は行く旅行があります。だから、私はそれを試した後にあなたに返信します。おかげで多くの仲間。 –

関連する問題