2017-09-08 10 views
0

YouTube와 같은 컨트롤러를 구현하고 싶습니다. 여기서 컨트롤러 상단 부분은 비디오를 재생하고 하단은 정지 된 상태를 유지합니다. 그리고 사용자가 장치를 회전시킬 때 Top 만 회전합니다. 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에 표시된 것처럼 플레이어의보기 만 회전해야합니다. 다른 뷰를 모두 그대로 유지 한 다음 하나의 뷰만 회전 할 수 있습니다. 하지만 상태 표시 줄을 고칠 수는 없습니다. 그것은 세로로 유지됩니다. 코드에서 변경할 수 없습니다. 내 제안은 가로 모드에서 상태 표시 줄을 숨기는 것입니다. 그것이 youtube가하는 것입니다. 괜찮 으면 코드를 업데이트 할 수 있습니다. – Bishan