2017-12-02 5 views
1

いいえ私はAVPlayerであるビューコントローラを1つ持っています。私は私がまだ肖像画にあるように、以前のコントローラを必要とする画面を閉じると、そのコントローラが1つのコントローラをランドスケープモードに回転させることができます

自由にランドスケープモードとポートレートに
import Foundation 
import UIKit 
import AVFoundation 
import AVKit 

class EventPromoVideoPlayer: UIViewController { 
    public var eventKey = "" 

    override var prefersStatusBarHidden: Bool { 
     return true 
    } 
    //URL of promo video that is about to be played 
    private var videoURL: URL 
    // Allows you to play the actual mp4 or video 
    var player: AVPlayer? 
    // Allows you to display the video content of a AVPlayer 
    var playerController : AVPlayerViewController? 

    init(videoURL: URL) { 
     self.videoURL = videoURL 
     super.init(nibName: nil, bundle: nil) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.view.backgroundColor = UIColor.gray 
     let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(swipeAction(_:))) 
     downSwipe.direction = .down 
     view.addGestureRecognizer(downSwipe) 


     //Setting the video url of the AVPlayer 
     player = AVPlayer(url: videoURL) 
     playerController = AVPlayerViewController() 

     guard player != nil && playerController != nil else { 
      return 
     } 
     playerController!.showsPlaybackControls = false 
     // Setting AVPlayer to the player property of AVPlayerViewController 
     playerController!.player = player! 
     self.addChildViewController(playerController!) 
     self.view.addSubview(playerController!.view) 
     playerController!.view.frame = view.frame 
     // Added an observer for when the video stops playing so it can be on a continuous loop 
     NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player!.currentItem) 
     //TODO: Need to fix frame of x and y 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 
     player?.play() 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     navigationController?.navigationBar.isHidden = true 
     tabBarController?.tabBar.isHidden = true 
    } 

    override var supportedInterfaceOrientations:UIInterfaceOrientationMask { 
     return UIInterfaceOrientationMask.all 
    } 
    // Allows the video to keep playing on a loop 
    @objc fileprivate func playerItemDidReachEnd(_ notification: Notification) { 
     if self.player != nil { 
      self.player!.seek(to: kCMTimeZero) 
      self.player!.play() 
     } 
    } 

    @objc func cancel() { 
     dismiss(animated: true, completion: nil) 
    } 

    @objc func swipeAction(_ swipe: UIGestureRecognizer){ 
     if let swipeGesture = swipe as? UISwipeGestureRecognizer { 
      switch swipeGesture.direction { 
      case UISwipeGestureRecognizerDirection.right: 
       print("Swiped right") 
       break 
      case UISwipeGestureRecognizerDirection.down: 
       print("Swiped Down") 
       dismiss(animated: true, completion: nil) 
       break 
      case UISwipeGestureRecognizerDirection.left: 
       print("Swiped left") 
       break 
      case UISwipeGestureRecognizerDirection.up: 
       print("Swiped up") 
       break 
      default: 
       break 
      } 
     } 
    } 
} 

を回転させることができるようにしたいです。ポートレートモードが私の設定の唯一の方向として固定されているので、これをそのままにしておきたいので、これは問題ではありません。しかし、私はこの1つの画面が自由に動くことができるようにしたい。どんなアイデアやヒントも高く評価されます。

アプリデリゲートメソッドが動作しません。 関数内でshouldAutoRotateまたはsupportedInterfaceOrientationsをオーバーライドしません。

答えて

0

以下のメソッドは、アプリケーションデリゲートで使用できます。

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 
    if let rootViewController = UIApplication.topViewController() { 
     if (rootViewController.responds(to: Selector(("canRotate")))) || String(describing: type(of: rootViewController)) == "AVFullScreenViewController" { 
      // Unlock landscape view orientations for this view controller 
      return .allButUpsideDown; 
     } 
    } 
    // Only allow portrait (standard behaviour) 
    return .portrait 
} 

ランドスケープとして表示するビューコントローラに以下のメソッドを追加します。

func canRotate() -> Void {} 

extension UIApplication { 

    class func topViewController(base: UIViewController? = (UIApplication.sharedApplication().delegate as! AppDelegate).window?.rootViewController) -> UIViewController? { 
    if let nav = base as? UINavigationController { 
     return topViewController(base: nav.visibleViewController) 
    } 
    if let tab = base as? UITabBarController { 
     if let selected = tab.selectedViewController { 
     return topViewController(base: selected) 
     } 
    } 
    if let presented = base?.presentedViewController { 
     return topViewController(base: presented) 
    } 
    return base 
} 
+0

topViewController?そのswift3の構文 –

+0

これは何も回されていません –

+0

はい私の電話を肖像画に変え、何も起こらなかった –

関連する問題