【发布时间】:2013-08-13 13:41:15
【问题描述】:
我有一个显示生成的 html 表的 UIWebView。当用户点击 html 表格中的单元格时,我的应用需要知道他们点击了哪个单元格,以及点击位置的 (x,y) 坐标,以便在该点显示一个弹出框。
我已经在我的 UIWebView 委托中实现了 shouldStartLoadWithRequest。在我的网页中,我嵌入了 javascript 代码,用于捕获触摸事件并在 URL 请求中传递触摸点的 (x,y) 坐标,如下所示:
var x, y;
function init()
{
// Add an event listener for touch events - set x and y to the coordinate of the touch point
document.addEventListener('touchstart', function(event) {
x = event.touches[0].clientX;
y = event.touches[0].clientY;
}, false);
}
function cellTapped(event)
{
window.location.href="file://myapp/dostuff?x=" + x + "&y=" + y;
}
在我的 html 表格中,每个单元格都会收到一个调用 cellTapped() 的 onclick 事件:
<td onclick="cellTapped(event)">...</td>
因此,每当用户触摸 UIWebView 中的任何位置时,我都会获得触摸点的坐标,并将其保存在 x 和 y 中。如果他们在其中一个表格单元格内触摸,我会收到触摸事件(设置 x 和 y),然后调用 cellTapped() 并设置 window.location.href,将 (x,y) 坐标传递到我的应用程序中。
这一切都很好。除非用户缩放或滚动了 UIWebView。当他们缩放或滚动时,我从 event.touches[0].clientX 和 event.touches[0].clientY 获得的 x 和 y 坐标偏离了一些不同数量的像素(随缩放量和方式而变化) Web 视图向上/向下或向左/向右滚动)。
有没有办法确定网页视图的缩放比例和滚动位置,以便我可以相应地调整我的 x 和 y 坐标? UIScrollView 中的 zoomScale 和 contentOffset 属性似乎没有在 UIWebView 中公开。
【问题讨论】: