2015-09-04 4 views
0

Я хочу создать пользовательский контроллер для MPMoviePlayerController. Все работает отлично с кнопкой, но когда я вращаю устройство, subview (видеоконтроллер) не обновляет макет, чтобы он соответствовал экрану.MPMoviePlayerController custom control (subview)

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

Как очистить Subview (viewControll - subview, который сделалButton и 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