I have a UIWebView that displays a generated html table. When the user clicks on a cell in the html table, my application should know which cell they listened to, and the coordinate (x, y) of the crane location, so that I can display a popover at that point.
I followed shouldStartLoadWithRequest in my UIWebView delta. On my webpage, I have built-in javascript code that captures the touch event and passes what should be the coordinate (x, y) of the affected point in the URL request as follows:
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; }
In my html table, each cell receives an onclick event that calls cellTapped ():
<td onclick="cellTapped(event)">...</td>
So whenever a user touches somewhere in a UIWebView, I get the coordinate of the touch point, which I save in x and y. If they touch one of the table cells, I get a touch event (which sets x and y), then cellTapped () is called, and I set window.location.href, passing the coordinate (x, y) to my application.
All of this works great. If the user has not enlarged or scrolled the UIWebView. When they are scaled or scrolled, the x and y coordinates that I get from event.touches [0] .clientX and event.touches [0] .clientY are turned off by a number of pixels (varies depending on the zoom scale and how far up / down or left / right, webpage browsing scrolls).
Is there a way to determine the zoom factor and scroll position of the web view so that I can adjust the x and y coordinates accordingly? The zoomScale and contentOffset of the UIScrollView do not seem to display in the UIWebView.