Override UIWebView’s Pinch Gesture (or any gesture)

In a recent application I needed to use the UIPinchGestureRecognizer on a UIWebview. However, UIWebviews use a UIScrollView which already has a pinch gesture attached to it. I could not find a solution so I came up with this:

1. Disable the pinch gesture recognizer on the web view’s scroll view. (below)
2. Enable your own gesture recognizer

for (UIGestureRecognizer *gesture in self.webView.scrollView.gestureRecognizers) {
    if([gesture isKindOfClass:[UIPinchGestureRecognizer class]]){
        [gesture setEnabled:NO];
        break;// don't waste time once found
    }
}

When you want to re-enable the web views default functionality:

1. Disable your gesture recognizer
2. Re-enable the pinch gesture recognizer on the web view’s scroll view.

for (UIGestureRecognizer *gesture in self.webView.scrollView.gestureRecognizers) {
    if([gesture isKindOfClass:[UIPinchGestureRecognizer class]]){
        [gesture setEnabled:YES];
        break;
    }
}

 

You could also remove and store the target on the existing scroll view pinch gesture, and add your own. Then replace it again when done. There may be many more ways to do this as well.

Caution:

This involves working within the structure of UIWebview, which is not guaranteed to always be the same across various versions of iOS. In this case, scroll view is an accessed property of a web view, and gestures are always added to that subview, but Apple doesn’t guarantee this (as far as I know).

One thought on “Override UIWebView’s Pinch Gesture (or any gesture)

  1. új

    whoah this weblog is excellent i love reading your articles.
    Stay up the good work! You understand, many persons are hunting around
    for this information, you can help them greatly.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *