如何实现像YouTube这样的视频控制器旋转到横向

时间:2017-09-08 13:28:46

标签: ios swift youtube rotation fullscreen

我想实现像YouTube这样的控制器。控制器的顶部部分播放视频,底部保持静态。当用户旋转设备时,Top旋转。

我尝试使用此代码实现它:

@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 个答案:

答案 0 :(得分:0)

这是解决问题的简单方法,

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
    }


}