2

I have a UIWebView hosted inside UIViewController, I manage to add the right\left gestures in the following way:

var swipeRight = UISwipeGestureRecognizer(target: self, action: "swiped:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.webView.addGestureRecognizer(swipeRight)

The problem that it's not working with up/down directions:

var swipeDown = UISwipeGestureRecognizer(target: self, action: "swiped:")
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
self.webView.addGestureRecognizer(swipeDown)

I think it's because of the scrolling and I also tried to add it to the scrollview but it still doesn't recognize the up/down gestures:

self.webView.scrollView.addGestureRecognizer(swipeDown)

UPDATE:
I tried Harry answer but it override the default action of swipe up/down and I can't scroll the page:

self.view.addGestureRecognizer(swipeUp)
self.view.addGestureRecognizer(swipeDown)

self.webView.scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeUp)
self.webView.scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeDown)
Dor Cohen
  • 16,769
  • 23
  • 93
  • 161

2 Answers2

3

Add gesture recogniser to self.view not UIWebView instance.

self.view.addGestureRecognizer(swipeUp)
self.view.addGestureRecognizer(swipeDown)

Add these two lines after adding up/down gesture recognisers to web view.

self.webView.scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeUp)
self.webView.scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeDown)
Jun
  • 3,422
  • 3
  • 28
  • 58
  • Thanks, now it's working but it override the default action of swipe up/down and I can't scroll the page – Dor Cohen Jan 14 '15 at 09:49
  • Sorry but I still can't scroll, I'm getting the gesture I defined but can't scroll up/down the UIWEBVIEW, what am I doing wrong? I edited my question – Dor Cohen Jan 15 '15 at 10:00
1

In case of someone is still working on it, this solved my problem :

webView.scrollView.panGestureRecognizer.cancelsTouchesInView = NO;

upGesture.direction = UISwipeGestureRecognizerDirectionUp;
upGesture.cancelsTouchesInView = NO;
upGesture.delegate = self;
[webView.scrollView addGestureRecognizer:upGesture];

downGesture.direction = UISwipeGestureRecognizerDirectionDown;
downGesture.cancelsTouchesInView = NO;
downGesture.delegate = self;
[webView.scrollView addGestureRecognizer:downGesture];



- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return [gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}