在UIWebView中向上/向下滑动手势不起作用

时间:2015-01-13 14:44:05

标签: ios swift

我在UIViewController中托管了一个UIWebView, 我设法通过以下方式添加右侧\左手势:

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

它无法使用上/下指示的问题:

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

我认为这是因为滚动,我还尝试将其添加到滚动视图但它仍然无法识别上/下手势:

self.webView.scrollView.addGestureRecognizer(swipeDown)

更新
我试过哈利回答,但它覆盖了向上/向下滑动的默认操作,我无法滚动页面:

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

self.webView.scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeUp)
self.webView.scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeDown)

2 个答案:

答案 0 :(得分:2)

self.view而不是UIWebView实例添加手势识别器。

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

在向网络视图添加/关闭手势识别器后添加这两行。

self.webView.scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeUp)
self.webView.scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeDown)

答案 1 :(得分:0)

如果有人还在努力,这就解决了我的问题:

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]];
}