Swift MPMoviePlayerController自定义控件(子视图)

Swift MPMoviePlayerController自定义控件(子视图),swift,orientation,mpmovieplayercontroller,Swift,Orientation,Mpmovieplayercontroller,我想为MPMoviePlayerController创建一个自定义控制器。按钮一切正常,但当我旋转设备时,子视图(视频控制器)不会更新布局以适应屏幕 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. moviePlayer = MPMoviePlayerController(contentURL: urlVideo)

我想为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{ $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)
}

您正在清理这些已存在的子视图吗?例如,doneButton和playButton。如何清理子视图(viewControll是包含doneButton和playButton的子视图)我很高兴能帮助您:)