2017-07-30 69 views
0

我有一個AVPlayerViewController的子類,我想在其上添加自定義控件。我成功地將自定義控件添加到AVPlayerViewController的頂部。但是,這些控件只能在短時間內響應。幾秒鐘後,單擊任何按鈕都不會觸發touchUpInside事件。iOS - 在AVPlayerViewController頂部添加控件

下面是我使用的代碼:

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

}

+0

確實[這] (https://stackoverflow.com/a/27253734/1971013),我發現在十秒內,幫助? –

+0

我之前也檢查過,但是這並不能解決我的問題。我正在成功添加控件,但touchUpInside事件在短時間後不會觸發。 – krypton36

回答

0

我加入以下兩行旁邊的addSubview功能解決了這個問題:

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