简体   繁体   English

WebView和UISwipeGestureRecognizer

[英]WebView and UISwipeGestureRecognizer

I am trying to have a UISwipeGestureRecognizer that is attached to a webview. 我正在尝试将UISwipeGestureRecognizer附加到UISwipeGestureRecognizer This works, but I need to also be able to scroll up and down the web view. 这可行,但我还需要能够上下滚动Web视图。

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. 如果我注释掉最后一行,则Webview滚动,但手势无法识别。 If I do not comment out the last line, then the gesture is recognized but the webview does not scroll. 如果我没有注释掉最后一行,则可以识别手势,但Webview不会滚动。

I would like to be able to scroll and have the gesture be recognized. 我希望能够滚动并识别手势。 Any ideas? 有任何想法吗? Thanks! 谢谢!

UIWebView has its own private views, which also has gesture recognizers attached. UIWebView有其自己的私有视图,还附加了手势识别器。 Hence, precedence rules keep any gesture recognizers added to a UIWebView from working properly. 因此,优先级规则使添加到UIWebView的所有手势识别器均无法正常工作。

One option is to implement the UIGestureRecognizerDelegate protocol and implement the method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer . 一种选择是实现UIGestureRecognizerDelegate协议,并实现gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer方法gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer Return YES from this method for other tap gestures. 对于其他轻击手势,从此方法返回YES。

This way you'll get your tap handler called, and the web view will still get its called. 这样,您将调用Tap处理程序,并且Web视图仍将被调用。

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, ind实现此委托方法,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM