简体   繁体   中英

iOS - Adding controls on top of AVPlayerViewController

I have a subclass of AVPlayerViewController that I want to add my custom controls on it. I successfully added my custom controls on top of the AVPlayerViewController. However, these controls are only responsive for a short time. After a few seconds, clicking any of the button won't trigger the touchUpInside event.

Below is the code I'm using:

class LSPlayerViewController : AVPlayerViewController {

private var btnPlay : UIButton!

func addControls() {
    self.showsPlaybackControls = false
    
    //PLAY BUTTON
    let btnPlayRect = CGRect(x: 0, y: 0, width: 19, height: 25)
    btnPlay = UIButton(frame: btnPlayRect)
    btnPlay.center = self.view.center
    btnPlay.setImage(#imageLiteral(resourceName: "PauseIcon"), for: .normal)
    btnPlay.addTarget(self, action: #selector(self.playButtonTapped(sender:)), for: .touchUpInside)
    btnPlay.alpha = 0
    btnPlay.isUserInteractionEnabled = true
    self.view.addSubview(btnPlay)
}

func playButtonTapped(sender: UIButton!) {
    if sender.isPlayIconOn == true {
        sender.setImage(#imageLiteral(resourceName: "PauseIcon"), for: .normal)
        self.player?.play()
    } else {
        sender.setImage(#imageLiteral(resourceName: "PlayIcon"), for: .normal)
        self.player?.pause()
    }
}

I solved the issue by adding the following two lines alongside the addSubview function:

self.addChildViewController(playerViewController)
playerView.addSubview(playerViewController.view)
playerViewController.didMove(toParentViewController: self)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM