当视频在WebView中播放时,将UIButton添加到iOS(Swift)中的AVPlayer

时间:2017-07-19 04:57:38

标签: ios swift webview uibutton avplayerviewcontroller

我正在整理一个包含webview的iOS应用程序(使用Swift)。用户将点击webview中的视频,视频将加载,此时我想在视频上显示一个按钮。我已经能够显示按钮,但它没有响应触摸,我不知道为什么。我把按钮做得很大,以确保它被击中。这是我的代码。我扩展了AVPlayerViewController。

提前感谢您的时间和耐心。

extension AVPlayerViewController {

    open override func viewDidLoad() {

        let btn = UIButton(type: .roundedRect)
        btn.backgroundColor = UIColor.red
        btn.setTitle("Do stuff", for: .normal)
        btn.frame = CGRect(x: 0, y: 330, width: 300, height: 300)
        btn.addTarget(self, action: #selector(self.pressButton(button:)), for: .touchUpInside)
        btn.isUserInteractionEnabled = true
        btn.isEnabled = true
        btn.clipsToBounds = true

        self.contentOverlayView?.addSubview((btn as? UIButton)!)
        self.showsPlaybackControls = false


    }


    func pressButton(button: UIButton) {
        print("Worked")
    }

}

Screen Shot of iOS App

1 个答案:

答案 0 :(得分:0)

AVPlayerViewController的扩展名中,您覆盖了viewDidLoad方法,这在我的视图中是不正确的。相反,您可以在扩展中添加按钮添加逻辑,并调用该方法,以便ypu初始化AVPlayerViewController

extension AVPlayerViewController {
func addButton() {
    let btn = UIButton(type: .roundedRect)
    btn.backgroundColor = UIColor.red
    btn.setTitle("Do stuff", for: .normal)
    btn.frame = CGRect(x: 0, y: 330, width: 300, height: 300)
    btn.addTarget(self, action: #selector(self.pressButton(button:)), for: .touchUpInside)
    btn.isUserInteractionEnabled = true
    btn.isEnabled = true
    btn.clipsToBounds = true

    self.contentOverlayView?.addSubview((btn as? UIButton)!)
    self.showsPlaybackControls = false
}

func pressButton(button: UIButton) {
    print("Worked")
}
}