Don Wilson
Don Wilson

Reputation: 2303

Using a UIGestureRecognizer in a UIWebView

Previously I've used the tap Detecting Window technique to detect taps in a UIWebView, but now I've tried to use gesture recognizers instead. The following code is in the viewDidLoad method of a view controller, which has a single UIWebView. This code compiles fine, but the handleTap method is never called. This seems like it should be simple.

// Configure a gesture recognizer to detect taps in the web view
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)];
[self.myWebView addGestureRecognizer:singleTap];    
[singleTap release];

[super viewDidLoad];

Upvotes: 3

Views: 1272

Answers (2)

lakim
lakim

Reputation: 592

Set your view controller as the recognizer delegate:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)];
recognizer.delegate = self;
[self.myWebView addGestureRecognizer:recognizer];

And enable simultaneous gesture recognition (as the UIWebView probably sets a few recognizers itself, yours are skipped) :

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

Upvotes: 6

Besi
Besi

Reputation: 22939

Since the UIWebView does already handle a lot of touch events, it might be a bit tricky to add your own gesture recognizer.

I don't know if the user has to interact with your web view but you might try to add a UIView on top of the UIWebView and add the gesture recognizer to this view. It should still propagate unrecognized touch events to the underlying web view, thus leaving its interactivity intact.

Upvotes: 0

Related Questions