1

I am trying to have a UISwipeGestureRecognizer that is attached to a webview. This works, but I need to also be able to scroll up and down the web view.

Here is my code:

UISwipeGestureRecognizer *upSwipeGesture = [[UISwipeGestureRecognizer alloc] 
initWithTarget:self action:@selector(tapAction)];
upSwipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
upSwipeGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:upSwipeGesture];

[webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:upSwipeGesture];

If I comment out the last line, then the webview scrolls but the gesture is not recognized. If I do not comment out the last line, then the gesture is recognized but the webview does not scroll.

I would like to be able to scroll and have the gesture be recognized. Any ideas? Thanks!

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
user2492064
  • 591
  • 1
  • 8
  • 21
  • 1
    Possible duplicate of [Does UIGestureRecognizer work on a UIWebView?](http://stackoverflow.com/questions/2909807/does-uigesturerecognizer-work-on-a-uiwebview) – Toseef Khilji Dec 01 '15 at 14:40

1 Answers1

1

UIWebView has its own private views, which also has gesture recognizers attached. Hence, precedence rules keep any gesture recognizers added to a UIWebView from working properly.

One option is to implement the UIGestureRecognizerDelegate protocol and implement the method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer. Return YES from this method for other tap gestures.

This way you'll get your tap handler called, and the web view will still get its called.

Try this,

UISwipeGestureRecognizer *upSwipeGesture = [[UISwipeGestureRecognizer alloc] 
initWithTarget:self action:@selector(tapAction)];
upSwipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
upSwipeGesture.cancelsTouchesInView = NO;
upSwipeGesture.delegate=self;
[self.view addGestureRecognizer:upSwipeGesture];

ind implement this delegate method,

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121