UILongPressGestureRecognizer将触摸传递给UIWebView

时间:2015-06-02 08:46:16

标签: ios objective-c uiwebview uigesturerecognizer

我将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,但经过长时间按超链接触摸传递到UIWebViewUIWebView点击此超链接并加载其他页面。

长按后如何禁用UIWebView的触摸?

2 个答案:

答案 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'"];   
}