如何禁用UIWebview中的放大镜?

发布于 2024-11-30 19:33:27 字数 69 浏览 1 评论 0原文

如何禁用在 UIWebView 上按住触摸时出现的放大镜?我不想禁用用户交互,但我不想让网络视图显示变焦玻璃。有什么想法吗?

How can I disable the magnifying glass that appears when you hold the touch on a UIWebView? I don't want to to disable user interaction but I don't want the webview to show that zoom glass. Any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鸢与 2024-12-07 19:33:27

不,放大镜与选择有着千丝万缕的联系。要禁用它,您必须完全禁用选择(您可以使用 -webkit-user-select: none 来执行此操作)。

No, the loupe is inextricably linked to selection. To disable it, you will have to disable selection entirely (you can use -webkit-user-select: none to do that).

请止步禁区 2024-12-07 19:33:27

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

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

我所做的是递归地遍历 UIWebView子视图并枚举它们的 手势识别器。每当我遇到 UILongPressGestureRecognizer,我设置了它的已启用

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

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

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

Because the accepted solution did not work for me, I had to look for other options and I found one.
Note that I don't know if Apple approves this technique. Use it at your own fear and risk.

(Ours wasn't rejected; I don't think Apple cares that much about you messing with UIWebView internals, but be warned.)

What I did was recursively walk down UIWebView subviews and enumerate their gestureRecognizers. Whenever I encounter a UILongPressGestureRecognizer, I set its enabled to NO.

This gets rid of the magnifying glass altogether and obviously disables any default long-press functionality.

It seems like iOS re-enables (or re-creates) these gesture recognizers whenever user begins to edit text.
Well, I don't mind using magnifying glass in text fields so I don't disable them immediately.

Instead, I wait for blur event on my text elements, and when it occurs, I walk the subview tree again.
Simple as that, and it works.

Bonjour°[大白 2024-12-07 19:33:27

因为我不知道如何使用 -webkit-user-select: none 我寻找其他方法。我偶然发现了这个 自定义上下文UIWebView 的菜单,然后我将其与 -webkit-user-select: none 结合起来。

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitUserSelect='none';"];
}

Because I don't know how to use -webkit-user-select: none I looked for other ways. And I stumble into this Customize the contextual menu of UIWebView then I combined it with -webkit-user-select: none.

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitUserSelect='none';"];
}
握住你手 2024-12-07 19:33:27

我发现仅 -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 的旧版本,我已将其放入万一。

I have found that -webkit-user-select: none; alone doesn't do the trick. Instead I have found a quite undocumented property -webkit-touch-callout

What I usually do with Phonegap apps is this:

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

Somewhere it was mentioned that -webkit-user-callout is a legacy version of -webkit-touch-callback, I have put this in just in case.

热血少△年 2024-12-07 19:33:27

对于我们的科尔多瓦和我所做的 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() 方法。

For our Cordova & Swift project I did:

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)
    {

    }

Note that this is my CDVPlugin class (or rather my custom version of it).

Make sure your config.xml file has:

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

This will ensure the init() method is called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文