我将UILongPressGestureRecognizer
添加到UIWebView
:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTouch:)];
longPress.delegate = self;
longPress.minimumPressDuration = 0.5;
longPress.cancelsTouchesInView = YES;
[_webView addGestureRecognizer:longPress];
cancelsTouchesInView
设为YES
,但经过长时间按超链接触摸传递到UIWebView
和UIWebView
点击此超链接并加载其他页面。
长按后如何禁用UIWebView
的触摸?
答案 0 :(得分:0)
您可以使用以下代码来阻止webview内容中的触摸链接 -
@property (assign, nonatomic) BOOL allowLoad;
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
return allowLoad;
}
- (void)webViewDidFinishLoad:(UIWebView*)webView {
allowLoad = NO;
}
不要忘记设置委托[self.webView setDelegate:self];
希望这会对你有所帮助
答案 1 :(得分:0)
实施以下手势委托方法以防止长按时触摸网页视图:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:self.webView])
{
//Ignore gesture when it's web view
return NO;
}
return YES;
}
<强>更新强>
请尝试使用以下代码 -
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none'; document.body.style.KhtmlUserSelect='none'"];
}