2017-04-08 25 views
0

我想在電影播放時旋轉我的視圖控制器。
我的問題可能是重複的,但當我發現堆棧溢出時,我嘗試了很多方法。但所有代碼都不起作用。可能是我把代碼以錯誤的方式。完全旋轉使用VLCPlayer播放電影時的橫向視圖3

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

我在鏈接的答案中有一個解決方案,但我調整了景觀。按照這些步驟,它會給你所需的功能。

斯威夫特3

在AppDelegate中:

/// 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:

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") 
    } 

} 

然後在所需的ViewController你想鎖定和旋轉方向,如您的電影控制器:

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

你可以將它添加到任何地方。但我會把它放在一個新的swift文件中,以便您可以在應用程序的任何位置訪問代碼,以防止其他時間旋轉和鎖定。 – 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

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

的viewDidLoad方法而在同一類中添加這些方法

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

是的兄弟。其實,我有旅行去。所以,我會在測試後回覆你。非常感謝。 –

相關問題