我正在整理一个包含webview的iOS应用(使用Swift)。用户将在Web视图中单击视频,然后将加载视频,这时我要在视频上显示一个按钮。我已经能够显示该按钮,但是它对触摸没有响应,我不确定为什么。我把按钮做得很大只是为了确保它被击中。这是我的代码。我扩展了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")
}
}
最佳答案
在您的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")
}
}
关于ios - 在WebView中播放视频时,将UIButton添加到iOS中的AVPlayer(快速),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45181303/