gpt4 book ai didi

swift - MPMoviePlayerController 自定义控件( subview )

转载 作者:行者123 更新时间:2023-11-30 14:06:00 25 4
gpt4 key购买 nike

我想为 MPMoviePlayerController 创建一个自定义 Controller 。使用按钮一切正常,但是当我旋转设备时, subview (视频 Controller )不会更新布局以适应屏幕。

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 - 当视频播放器旋转回纵向时(视频播放器中间仍然有一个红色按钮) enter image description here

最佳答案

当执行showController()函数时,您将在 View 层次结构中创建一个新的doneButton和playButton。因此,当旋转到横向时,您将在同一位置有两个完成按钮,在不同的 y 轴上有两个播放按钮。

快速修复它:

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)
}

关于swift - MPMoviePlayerController 自定义控件( subview ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32396110/

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