如何禁用在 UIWebView 上按住触摸时出现的放大镜?我不想禁用用户交互,但我不希望 webview 显示缩放玻璃。有任何想法吗?
5 回答
不,放大镜与选择密不可分。要禁用它,您必须完全禁用选择(您可以使用它-webkit-user-select: none
来执行此操作)。
因为接受的解决方案对我不起作用,所以我不得不寻找其他选择,我找到了一个。
请注意,我不知道 Apple 是否批准这种技术。在您自己的恐惧和风险中使用它。
(我们的没有被拒绝;我认为 Apple 不会那么在意你弄乱UIWebView
内部结构,但请注意。)
我所做的是递归地遍历UIWebView
子视图并枚举它们的gestureRecognizers
. 每当我遇到 aUILongPressGestureRecognizer
时,我将其设置enabled
为NO
。
这完全摆脱了放大镜,显然禁用了任何默认的长按功能。
每当用户开始编辑文本时,iOS 似乎都会重新启用(或重新创建)这些手势识别器。
好吧,我不介意在文本字段中使用放大镜,所以我不会立即禁用它们。
相反,我等待blur
我的文本元素上的事件,当它发生时,我再次遍历子视图树。
就这么简单,而且有效。
因为不知道怎么用,-webkit-user-select: none
所以找了其他方法。我偶然发现了这个自定义 UIWebView 的上下文菜单,然后我将它与-webkit-user-select: none
.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitUserSelect='none';"];
}
我发现-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
,我把它放在以防万一。
对于我们的 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()
调用该方法。