2017-09-08 16 views
0

YouTubeのようなコントローラを実装したいと思います。ここでは、コントローラの上部がビデオを再生し、下部が静止しています。ユーザーがデバイスを回転させるとトップが回転するだけです。YouTubeのように、風景を回転させながらビデオコントローラを実装する方法

私はこのコードでそれを実装しようとした:

@IBOutlet weak var myView: UIView! 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     NotificationCenter.default.addObserver(self, selector: #selector(self.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) 
    } 

    @objc func rotated() { 
     if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) { 
      UIView.animate(withDuration: 0.9, animations: { 
       self.myView.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2) 
       let rect = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height) 
       self.myView.frame = rect 
      }) 
     } 
    } 

そして、私はこの結果を持っている:

enter image description here

問題が間違った側に縦向きとステータスバーにまだコントローラです。

私はこのことがより良い実装だと思います。

してください、答えのため

これはあなたの問題のためのシンプルなソリューションです

答えて

1

import UIKit 

class ViewController: UIViewController { 

    let RotatingView :UIView = UIView() 
    let TextLabel :UILabel = UILabel() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 


     self.RotatingView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: (self.view.frame.size.height/3)) 
     self.RotatingView.backgroundColor = UIColor.black 



     self.TextLabel.text = "Rotating View" 
     self.TextLabel.textColor = UIColor.white 
     self.TextLabel.textAlignment = .center 
     self.TextLabel.frame = CGRect(x: 0, y: self.RotatingView.frame.size.height/2, width: self.RotatingView.frame.width, height: 20) 

     self.RotatingView.addSubview(self.TextLabel) 
     self.view.addSubview(self.RotatingView) 

     NotificationCenter.default.addObserver(self, selector: #selector(rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) 


    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func rotated(){ 
     switch UIDevice.current.orientation { 
     case .landscapeLeft, .landscapeRight: 
      self.RotatingView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: (self.view.frame.size.height)) 
      self.TextLabel.frame = CGRect(x: 0, y: self.RotatingView.frame.size.height/2, width: self.RotatingView.frame.width, height: 20) 
     default: 
      self.RotatingView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: (self.view.frame.size.height/3)) 
      self.TextLabel.frame = CGRect(x: 0, y: self.RotatingView.frame.size.height/2, width: self.RotatingView.frame.width, height: 20) 
     } 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(true) 
     self.setNeedsStatusBarAppearanceUpdate() 
    } 

    override var prefersStatusBarHidden : Bool { 
     return false 
    } 


} 
+0

感謝を助けて! しかし、このソリューションは、コントローラ内のすべてのビューを回転します。 私のgif –

+0

@ SergeiRに示されているように、プレイヤーのビューだけを回転させる必要があります。他のすべてのビューをそのまま維持し、1つのビューのみを回転することが可能です。しかし、ステータスバーを修正することはできません。それは肖像画のままです。コードから変更することはできません。私の提案は、ランドスケープモードでステータスバーを非表示にすることです。それはyoutubeがやっていることです。あなたがそれで大丈夫なら、私はコードを更新することができます。 – Bishan

関連する問題