Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
like image 765
Don Wilson Avatar asked Mar 04 '26 07:03

Don Wilson


1 Answers

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;
}
like image 75
lakim Avatar answered Mar 05 '26 19:03

lakim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!