Search code examples
iosuikituigesturerecognizer

How to detect only short taps


In my view controller, I have a WKWebView and I use a UITapGestureRecognizer to detect taps on the webview in order to show/hide the navigation and status bars. I also use the taps+javascript to detect when special parts of the web content are tapped.

WKWebView uses long-presses to allow the user to select text.

Super-long-presses work great. The web content is selected by WKWebView as expected and life is good.

My problem is when the user applies "shorter"-long-presses, because the UITapGestureRecognizer recognizes them as taps. WKWebView selects the text, so it seems like the user pressed long enough, but upon release the UITapGestureRecognizer fires the designated action and my code to show/hide the navigation bar kicks in.

What I want is to only show/hide the navigation bar when the user applies a very short tap. If they touch long enough for WKWebView to select text, I want to let WKWebView handle it.

I was hoping to filter taps by touch duration, but I haven't been able to find a way to determine this information.

Am I missing something? Can I achieve this with a UITapGestureRecognizer or do I need a different approach?

I'm targetting iOS 8 & 9.


Solution

  • If you have access to the gesture recognizer used by the WKWebView that selects the text, then you can setup your short tap recognizer to require that WKWebView recognizer to fail, using requireGestureRecognizerToFail:.

    If you don't have access to the gesture recognizer used by the WKWebView, then you can take this hackier approach:

    1. Create a UILongPressGestureRecognizer. This long press recognizer doesn't do anything on its own, but it's needed by step 2.
    2. In your short tap recognizer, use requireGestureRecognizerToFail:, specifying the UILongPressGestureRecognizer from step 1.

    Note, this second approach only has a shot of working if WKWebView is using a UILongPressGestureRecognizer under the hood.