2015-09-04 42 views
0

我想爲MPMoviePlayerController創建自定義控制器。一切正常,按鈕,但當我旋轉設備,子視圖(視頻控制器)不會更新佈局,以適應屏幕。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 - 當視頻播放器旋轉回縱向(還有一個紅色的在視頻播放器的中間的按鈕) enter image description here

+0

你清理這些存在的子視圖?例如。 doneButton和playButton。 – Bannings

+0

如何清理子視圖(viewControll是子視圖有doneButton和playButton) –

回答

0

showController()功能執行,您創建一個新的doneButton併爲playButton到您的視圖層次。所以當旋轉到橫向時,您將有兩個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) 
} 
+0

謝謝,它的作品! –

+0

我很高興幫助你:) – Bannings