Assuming you are using the full screen AVPlayerViewController, you can add a UITapGestureRecognizer to the controller view. But some UIGestureRecognizerDelegate methods were needed. I am adding subview to the AVPlayerViewController view and not to the contentOverlayView.
let controller = AVPlayerViewController() controller.player = self.player let yourView = controller.view let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(VideoPlayer.onCustomTap(_:))) tapGestureRecognizer.numberOfTapsRequired = 1; tapGestureRecognizer.delegate = self yourView.addGestureRecognizer(tapGestureRecognizer) self.yourSubView = YourView() controller.view.addSubview(yourSubView) parentViewController.modalPresentationStyle = .FullScreen; parentViewController.presentViewController(controller, animated: false, completion: nil)
Add a UIGestureRecognizerDelegate to your class and make sure that the height and width of any touch is the height and width of the screen. This, although obviously hacky, is the only way I have found it to work closely. The player controls how to disappear 5 seconds into the game and appears again at the end ... this can be allowed with observers on AVPlayer. I tried to go this route, and things like the ffwd button will make me abandon this for the user player.
extension VideoPlayer: UIGestureRecognizerDelegate { func onCustomTap(sender: UITapGestureRecognizer) { if yourSubView.alpha > 0{ UIView.animateWithDuration(0.5, animations: { self.yourSubView.alpha = 0; }) } else { UIView.animateWithDuration(0.5, animations: { self.yourSubView.alpha = 1; }) } } public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool { if let _touchView = touch.view { let screenRect:CGRect = UIScreen.mainScreen().bounds let screenWidth :CGFloat = screenRect.size.width; let screenHeight:CGFloat = screenRect.size.height; if _touchView.bounds.height == screenHeight && _touchView.bounds.width == screenWidth{ return true } } return false } public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } }
source share