当UIWebView
我长按它时,我需要访问 DOM 元素的属性(来自 SVG 图)。为此,我添加了UILongPressGestureRecognizer
以下内容:
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action: @selector(longPress:)];
[self.webView addGestureRecognizer: longPress];
当我长按视图时,将调用处理程序,我从中调用 JS 函数:
- (void) longPress: (UIGestureRecognizer *) gesture {
CGPoint curCoords = [gesture locationInView:self.webView];
if (!CGPointEqualToPoint(curCoords, self.lastLongPress)) {
self.lastLongPress = curCoords;
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"longPress(%f, %f)", curCoords.x, curCoords.y]];
}
}
这是我的 JS 处理程序:
function longPress(x, y) {
x = x + window.pageXOffset;
y = y + window.pageYOffset;
var element = svgDocument.elementFromPoint(x, y);
alert(element.localName + ' ' + x + ' ' + y + ' ' + window.innerWidth + ' ' + window.innerHeight);
}
但是,UIWebView
坐标似乎是 != 来自 DOM 坐标(我单击的位置与警报中显示的 localName 不对应)。我已经设法弄清楚UIWebView
坐标和 JS 之间存在 +/- 1.4 的因子(通过单击屏幕的右下角并将这些值与window.innder{Width,Height}
.
我的猜测是UIWebView
最初可能会应用默认的缩放比例,但我找不到这个值对应的值。
此外,当用户实际缩放/移动页面时,我还需要一种方法来完成这项工作。
有谁知道我做错了什么?
谢谢,