I know this could be a replicate of this question AVPlayer UITapGestureRecognizer not working
However, I still post it because I don't have enough reps to comment on the original answer.
The question is how can I add UITapGestureRecognizer
to the AVPlayerViewController
. Things I've tried:
let wake_overlayview = UITapGestureRecognizer(target: self, action:#selector(self.wake_YCOverlayView))
(1) Add gesture recognizer to the view of the controller. This doesn't work:
self.player.view.addGestureRecognizer(wake_overlayview)
(2) Add it to the contentOverlayView:
self.player.contentOverlayView?.addGestureRecognizer(wake_overlayview)
This solution seems worked in this answer https://stackoverflow.com/a/48797984/4342489. However, it doesn't work for me...
(3) Initialize a UIView, add the gestureRecognizer on it, then add this view to the AVPlayer.view subview. This doesn't work:
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) add the recognizer to the first subview of player.view. This works on ios 10 but not 11 since in 11, player.view doesn't have any subview...
self.player.view.subviews[0].addGestureRecognizer(wake_overlayview)
Looking at your code it looks like you subclass AVPlayerViewController. This is not recommended. From Apple documentation:
Important
Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behavior.
But, if you still want to subclass AVPlayerViewController you can get touches by overriding touchesBegan:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesBegan")
}