MPMoviePlayerController 自定义控件(子视图)
MPMoviePlayerController custom control (subview)
我想为 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 - 当视频播放器旋转到横向时
3 - 当视频播放器旋转回纵向时(视频播放器中间仍然有一个红色按钮)
当 showController()
函数执行时,您会在视图层次结构中创建一个新的 doneButton 和 playButton。所以当旋转到横向时,你将在同一位置有两个 doneButton,在不同的 y 轴上有两个 playButton。
快速修复:
func showController() {
self.controllerView.subviews.map{ [=10=].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)
}
我想为 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 - 当视频播放器旋转到横向时
3 - 当视频播放器旋转回纵向时(视频播放器中间仍然有一个红色按钮)
当 showController()
函数执行时,您会在视图层次结构中创建一个新的 doneButton 和 playButton。所以当旋转到横向时,你将在同一位置有两个 doneButton,在不同的 y 轴上有两个 playButton。
快速修复:
func showController() {
self.controllerView.subviews.map{ [=10=].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)
}