Search code examples
iphoneipaduiwebviewtouch

How to display a view when webview is touched continously for 10 sec?


I am displaying a pub or a pdf file in webview. But I am not able to detect the touch on the webview.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

above method is called when I touch any text of the pdf or pub file. How can I detect the click on the webview which is displaying a pdf or a pub file.

This is my code:

- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://dblog.com.au"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPressGestureRecognizer.minimumPressDuration = 6.0;
[self.webView addGestureRecognizer:longPressGestureRecognizer];
[longPressGestureRecognizer release];

}

- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"Long press detected");
 }

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

Please check it, did I make any mistake?


Solution

  • Try something like:

    UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    longPressGestureRecognizer.minimumPressDuration = 10.0;
    [self.webView addGestureRecognizer:longPressGestureRecognizer];
    [longPressGestureRecognizer release];
    

    And then:

    - (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer {
        NSLog(@"Long press detected");
    }
    

    EDIT:

    Add this:

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

    But pressing same place for 10 seconds is a really long time... the default value for a long press is 0.4 seconds. Keep in mind that if the finger is dragged it is no longer a long press..