2017-09-08 6 views
0

Je veux mettre en œuvre un contrôleur comme YouTube. Où Partie supérieure du contrôleur joue la vidéo et le fond reste statique. Et lorsque l'utilisateur fait pivoter l'appareil, seul Top obtient une rotation.Comment mettre en œuvre un contrôleur vidéo comme YouTube avec rotation au paysage

J'ai essayé de le mettre en œuvre avec ce code:

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

Et je ce résultat:

enter image description here

Le problème est contrôleur toujours en orientation portrait et barre d'état du mauvais côté.

Je pense qu'il y a une meilleure implémentation pour ce truc.

S'il vous plaît, aidez-

Répondre

1

Ceci est une solution simple pour votre problème,

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

Merci pour la réponse! Mais cette solution fait tourner toutes les vues dans le contrôleur. Je dois faire pivoter uniquement la vue du lecteur, comme indiqué sur mon gif –

+0

@SergeiR. Il est possible de conserver toutes les autres vues telles qu'elles sont et de ne faire pivoter qu'une vue. Mais alors vous ne pouvez pas réparer la barre d'état. Il restera en portrait. Vous ne pouvez pas le changer du code. Ma suggestion est de cacher la barre d'état en mode paysage. C'est ce que fait youtube. Si vous êtes d'accord, je peux mettre à jour le code. – Bishan