我想为MPMoviePlayerController创建一个自定义控制器。使用按钮一切正常,但是当我旋转设备时,子视图(视频控制器)不会更新布局以适应屏幕。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
moviePlayer = MPMoviePlayerController(contentURL: urlVideo)
moviePlayer.movieSourceType = MPMovieSourceType.Unknown
moviePlayer.view.frame = self.view.bounds
moviePlayer.scalingMode = MPMovieScalingMode.None
moviePlayer.controlStyle = MPMovieControlStyle.None
moviePlayer.shouldAutoplay = true
moviePlayer.view.backgroundColor = UIColor.blackColor()
self.view.addSubview(moviePlayer.view)
moviePlayer.prepareToPlay()
moviePlayer.play()
var tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGestureRecognizer")
tapGestureRecognizer.delegate = self
self.view.addGestureRecognizer(tapGestureRecognizer)
// Do any additional setup after loading the view.
self.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
// NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "updatePlaybackTime:", userInfo: nil, repeats: true)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("playerPlaybackDidFinish:"),
name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)
NSNotificationCenter.defaultCenter() .addObserver(self, selector: "movieOrientationChanged", name: UIDeviceOrientationDidChangeNotification, object: nil)
self.showController()
}
func showController() {
doneButton = UIButton(frame: CGRectMake(0, 22, 50, 30))
doneButton.backgroundColor = UIColor.redColor()
doneButton.tintColor = UIColor.blackColor()
doneButton.setTitle("Done", forState: .Normal)
doneButton.addTarget(self, action: "exitVideo:", forControlEvents: UIControlEvents.TouchUpInside)
self.controllerView.addSubview(doneButton)
playButton = UIButton(frame: CGRectMake(0, self.view.bounds.size.height - 30, 60, 30))
playButton.backgroundColor = UIColor.redColor()
playButton.tintColor = UIColor.blackColor()
playButton.addTarget(self, action: "pauseVideo:", forControlEvents: UIControlEvents.TouchUpInside)
self.controllerView.addSubview(playButton)
self.moviePlayer.view.addSubview(self.controllerView)
}
func movieOrientationChanged() {
moviePlayer.view.frame = self.view.bounds
controllerView.frame = self.moviePlayer.view.bounds
self.showController()
}
1 - 打开视频播放器时
2 - 将视频播放器旋转为横向
答案 0 :(得分:0)
执行showController()
函数时,您会在视图层次结构中创建一个新的doneButton和playButton。所以当旋转到横向时,你将在同一个位置有两个doneButton,在不同的y轴上有两个playButton。
快速修复:
func showController() {
self.controllerView.subviews.map{ $0.removeFromSuperview() } // Added this line
doneButton = UIButton(frame: CGRectMake(0, 22, 50, 30))
doneButton.backgroundColor = UIColor.redColor()
doneButton.tintColor = UIColor.blackColor()
doneButton.setTitle("Done", forState: .Normal)
doneButton.addTarget(self, action: "exitVideo:", forControlEvents: UIControlEvents.TouchUpInside)
self.controllerView.addSubview(doneButton)
playButton = UIButton(frame: CGRectMake(0, self.view.bounds.size.height - 30, 60, 30))
playButton.backgroundColor = UIColor.redColor()
playButton.tintColor = UIColor.blackColor()
playButton.addTarget(self, action: "pauseVideo:", forControlEvents: UIControlEvents.TouchUpInside)
self.controllerView.addSubview(playButton)
self.moviePlayer.view.addSubview(self.controllerView)
}