Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a UITapGestureRecognizer to a UIWebView

In iOS, is it possible to put a tap recognizer on a UIWebView, so that when someone single-taps the web view, an action gets performed?

Code below doesn't seem fire handleTap when I tap my webView.

Thanks.

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                      action:@selector(handleTap)];
tap.numberOfTapsRequired = 1;

[webView addGestureRecognizer:tap];
[tap release]; 


-(void) handleTap {
    NSLog(@"tap");
}
like image 356
user768339 Avatar asked May 24 '11 19:05

user768339


People also ask

How to add a tap gesture to an imageview?

Adding a Tap Gesture Recognizer to an Image View in Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the image view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.

What is tap gesture?

The Tap gesture detects a deliberate tap on a component, in a manner that is more restrictive than onPress . Tap imposes the following additional restrictions with respect to the initial down and final pointer up events: They must occur within a short distance, to rule out drag-like gestures.


3 Answers

I found that I had to implement this method to get it to work:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
like image 146
Bryan Avatar answered Oct 20 '22 08:10

Bryan


Your UIViewController subclass should implement UIGestureRecognizerDelegate and return YES from gestureRecognizer: shouldReceiveTouch: when appropriate.

Then you can assign it to the delegate property of your UIGestureRecognizer.

tap.delegate = self;
like image 26
highlycaffeinated Avatar answered Oct 20 '22 09:10

highlycaffeinated


  1. Add UIGestureRecognizerDelegate to view controller

    <UIGestureRecognizerDelegate>
    
  2. Add Tap Gesture to Webview.

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(flip)];
    tap.numberOfTapsRequired = 1;
    tap.delegate = self;
    [_webview addGestureRecognizer:tap];
    
  3. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer    shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        return YES;
    }
    
like image 30
Kiran S Avatar answered Oct 20 '22 07:10

Kiran S