gpt4 book ai didi

ios - 如何实现像 YouTube 这样的视频 Controller 旋转到横向

转载 作者:行者123 更新时间:2023-11-28 08:02:48 25 4
gpt4 key购买 nike

我想实现像 YouTube 这样的 Controller 。 Controller 的顶部播放视频,底部保持静止。当用户旋转设备时,只有顶部会旋转。

我试着用这段代码来实现它:

@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

问题是 Controller 仍然处于纵向并且状态栏在错误的一侧。

我认为这个东西有更好的实现方式。

求助

最佳答案

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

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
}


}

关于ios - 如何实现像 YouTube 这样的视频 Controller 旋转到横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46117639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com