2

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)
Rong Gong
  • 85
  • 7

1 Answers1

2

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")
}
torof
  • 368
  • 1
  • 12
  • Thanks, a follow-up question. How can I ask touchesBegan to call a selector function? Something like `UITapGestureRecognizer(target: self, action:#selector(self.wake_YCOverlayView))` can call `self.wake_YCOverlayview` function. Can touchesBegan also do this? – Rong Gong Apr 14 '18 at 21:52
  • Yes, you can call other functions inside touchesBegan just like normal: self.wake_YCOverlayview(). If you need the touch information in the wake_YCOverlayview function, just pass the UITouch set as a parameter. You might want to check out the documentation of UIResponder (which includes touchesBegan): https://developer.apple.com/documentation/uikit/uiresponder – torof Apr 14 '18 at 22:42
  • In such way, I think the function wake_YCOverlayview should be defined in the same class as touchesBegan. My question is that can I defined wake_YCOverlayview in the class where I initiate the AVPlayerViewController? I can work around this by notification. However, I think it's not an optimal solution... – Rong Gong Apr 14 '18 at 23:25
  • 1
    You can solve this by delegation, but I think you are much better of making your own view controller with an AVPlayer. Then you'll need to handle all the player controls yourself, but if you have already added an overlay this might be what you want? – torof Apr 15 '18 at 08:05
  • Yes, I have a simple overlay, it is automatically removed from the avplayerviewcontroller.view if we don't touch the screen in a few minutes. My goal is to use touch gesture to re-add this overlay to the avplayer view. What I am doing now is sending a notification from the override touchsBegin function. I don't need to handle the original player controls, such as play, forward, backward buttons. I leave them as they are. You mentioned delegation. Could you give more clue about it, a link? I am not familiar with it. Thanks! – Rong Gong Apr 15 '18 at 12:02
  • For a good understanding of core Swift components like delegation, I would recommend the iOS 11 development course videos from Standford University: https://itunes.apple.com/us/course/developing-ios-11-apps-with-swift/id1309275316 – torof Apr 16 '18 at 09:35