AVPlayerViewController UITapGestureRecognizer 在 ios 11 中不工作

AVPlayerViewController UITapGestureRecognizer not working in ios 11

我知道这可能是这个问题的重复

但是,我仍然 post 因为我没有足够的代表来评论原始答案。

问题是如何将 UITapGestureRecognizer 添加到 AVPlayerViewController。我尝试过的事情:

let wake_overlayview = UITapGestureRecognizer(target: self, action:#selector(self.wake_YCOverlayView))

(1) 将手势识别器添加到控制器的视图中。这不起作用:

self.player.view.addGestureRecognizer(wake_overlayview)

(2) 添加到contentOverlayView:

self.player.contentOverlayView?.addGestureRecognizer(wake_overlayview)

此解决方案似乎适用于此答案 。但是,它对我不起作用...

(3)初始化一个UIView,在上面添加gestureRecognizer,然后将这个view添加到AVPlayer.view子view中。这不起作用:

self.gesture_view.frame = CGRect(x: 0,
                                 y: 0,
                                 width: self.player.view.frame.size.width,
                                 height: self.player.view.frame.size.height)

self.player.view.addSubview(self.gesture_view) 
self.player.view.bringSubview(toFront: self.gesture_view)  
self.gesture_view.addGestureRecognizer(wake_overlayview)  
self.gesture_view.isUserInteractionEnabled = true

(4) 将识别器添加到player.view的第一个子视图中。这适用于 ios 10 但不适用于 11,因为在 11 中,player.view 没有任何子视图...

self.player.view.subviews[0].addGestureRecognizer(wake_overlayview)

查看您的代码,您似乎继承了 AVPlayerViewController。不推荐这样做。来自 Apple documentation:

Important

Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behavior.

但是,如果您仍想子类化 AVPlayerViewController,您可以通过重写 touchesBegan 来获取触摸:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("touchesBegan")
}