Detect single tap in UIWebView, but still support text selection and links

后端 未结 2 738
灰色年华
灰色年华 2021-02-03 14:41

I\'m using JavaScript to detect taps in a page I\'m showing in a UIWebView, like so:


      
2条回答
  •  萌比男神i
    2021-02-03 15:21

    There is no need to use Javascript for this, it's overkill when the UIGestureRecognizerDelegate has adequate methods. All you need to do is make sure that when text selection is taking place, the tap recogniser isn't triggered.

    - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        BOOL hasTap = ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] ||
                   [otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]);
        BOOL hasLongTouch = ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] ||
                         [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]);
        if (hasTap && hasLongTouch) {
            // user is selecting text
            return NO;
        }
        return YES;
    }
    

    That takes care of text selection, and links should work fine anyway (at least they do for me).

提交回复
热议问题