12

如何禁用在 UIWebView 上按住触摸时出现的放大镜?我不想禁用用户交互,但我不希望 webview 显示缩放玻璃。有任何想法吗?

4

5 回答 5

8

不,放大镜与选择密不可分。要禁用它,您必须完全禁用选择(您可以使用它-webkit-user-select: none来执行此操作)。

于 2011-08-23T06:58:25.400 回答
7

因为接受的解决方案对我不起作用,所以我不得不寻找其他选择,我找到了一个。
请注意,我不知道 Apple 是否批准这种技术。在您自己的恐惧和风险中使用它。

(我们的没有被拒绝;我认为 Apple 不会那么在意你弄乱UIWebView内部结构,但请注意。)

我所做的是递归地遍历UIWebView子视图并枚举它们的gestureRecognizers. 每当我遇到 aUILongPressGestureRecognizer时,我将其设置enabledNO

这完全摆脱了放大镜,显然禁用了任何默认的长按功能。

每当用户开始编辑文本时,iOS 似乎都会重新启用(或重新创建)这些手势识别器。
好吧,我不介意在文本字段中使用放大镜,所以我不会立即禁用它们。

相反,我等待blur我的文本元素上的事件,当它发生时,我再次遍历子视图树。
就这么简单,而且有效。

于 2012-08-29T12:20:17.963 回答
3

因为不知道怎么用,-webkit-user-select: none所以找了其他方法。我偶然发现了这个自定义 UIWebView 的上下文菜单,然后我将它与-webkit-user-select: none.

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitUserSelect='none';"];
}
于 2012-02-24T06:57:53.850 回答
2

我发现-webkit-user-select: none;仅凭这一点并不能解决问题。相反,我发现了一个非常无证的属性-webkit-touch-callout

我通常使用 Phonegap 应用程序是这样的:

body, body * {
    -webkit-user-select: none !important;
    user-select: none !important;
    -webkit-user-callout: none !important;
    -webkit-touch-callout: none !important;
}
input, textarea {
    -webkit-user-select: text !important;
    user-select: text !important;
    -webkit-user-callout: default !important;
    -webkit-touch-callout: default !important;
}

某处提到这-webkit-user-callout是 的旧版本-webkit-touch-callback,我把它放在以防万一。

于 2014-09-04T10:48:32.770 回答
0

对于我们的 Cordova & Swift 项目,我做了:

override init!(webView theWebView: UIWebView!)
    {            
        super.init(webView: theWebView)

        removeLoupe()
    }

    /**
        Removes the magnifying glass by adding a long press gesture that overrides the inherent one that spawns
        a the loupe by default.
    */
    private func removeLoupe()
    {
        let views = webView?.subviews
        if (views == nil || views?.count == 0)
        {
            return
        }

        let longPress = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
        longPress.minimumPressDuration = 0.045
        longPress.allowableMovement = 100.0

        for view in views!
        {
            if (view.isKindOfClass(UIScrollView))
            {
                let subViews = view.subviews
                let browser = subViews[0]
                browser.addGestureRecognizer(longPress)
                break;
            }
        }
    }

    /**
       Hack to override loupe appearence in webviews.
    */
    func handleLongPress(sender:UILongPressGestureRecognizer)
    {

    }

请注意,这是我的CDVPlugin课程(或者更确切地说是我的自定义版本)。

确保您的 config.xml 文件具有:

<feature name="CustomCDVPlugin">
    <param name="ios-package" value="CustomCDVPlugin" />
    <param name="onload" value="true" />
</feature>

这将确保init()调用该方法。

于 2015-10-14T20:22:40.620 回答