【发布时间】:2011-08-23 06:54:37
【问题描述】:
如何禁用在 UIWebView 上按住触摸时出现的放大镜?我不想禁用用户交互,但我不希望 webview 显示缩放玻璃。有什么想法吗?
【问题讨论】:
-
你还想修改 webview 吗?
标签: iphone uiwebview magnification
如何禁用在 UIWebView 上按住触摸时出现的放大镜?我不想禁用用户交互,但我不希望 webview 显示缩放玻璃。有什么想法吗?
【问题讨论】:
标签: iphone uiwebview magnification
不,放大镜与选择密不可分。要禁用它,您必须完全禁用选择(您可以使用-webkit-user-select: none 来执行此操作)。
【讨论】:
因为接受的解决方案对我不起作用,所以我不得不寻找其他选项并找到了一个。
请注意,我不知道 Apple 是否批准这种技术。使用它需要您自担恐惧和风险。
(我们的没有被拒绝;我认为 Apple 不会那么在意你搞乱UIWebView 内部结构,但请注意。)
我所做的是递归地遍历UIWebView 子视图并枚举它们的gestureRecognizers。每当我遇到UILongPressGestureRecognizer 时,我都会将其enabled 设置为NO。
这完全摆脱了放大镜,显然禁用了任何默认的长按功能。
iOS 似乎会在用户开始编辑文本时重新启用(或重新创建)这些手势识别器。
好吧,我不介意在文本字段中使用放大镜,所以我不会立即禁用它们。
相反,我在我的文本元素上等待blur 事件,当它发生时,我再次遍历子视图树。
就这么简单,而且有效。
【讨论】:
因为我不知道如何使用-webkit-user-select: none,所以我寻找其他方法。我偶然发现了这个Customize the contextual menu of UIWebView,然后我将它与-webkit-user-select: none结合在一起。
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitUserSelect='none';"];
}
【讨论】:
-webkit-user-select: none 是一个 CSS 属性。因此,您需要使用您发布的代码,或者如果您有权访问 web 视图中显示的网络内容,则将其包含在 CSS 中。
我发现单独使用-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() 方法。
【讨论】: