How to get (X, Y) touch coordinate in UIWebView

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.

+6
source share
2 answers

EDIT . In iOS 5 and above, the scrollView property of the UIWebView is open and accessible, so this becomes a problem without problems. In my case, I still need to support devices running iOS 4 (believe it or not ...), so the next solution is for older versions.

Going through the subzones of my UIWebView , I can find the base UIScrollView , and then use its zoomScale and contentOffset properties to find the zoom and scroll position:

 for (UIView *view in myWebView.subviews) { if ([view isKindOfClass:[UIScrollView class]]) { // Get UIScrollView object scrollview = (UIScrollView *) view; // Find the zoom and scroll offsets float zoom = scrollView.zoomScale; float xOffset = scrollView.contentOffset.x; float yOffset = scrollView.contentOffset.y; } } 

I don’t know whether Apple will approve it for presentation in the app store, as I assume that they had reasons not to expose the underlying UIScrollView object, but it really solves my problem. My application is licensed under an Enterprise license, so presenting an application store is not a problem for me.

+1
source

Use the UIGestureRecognizerDelegate method:

Add UIGestureRecognizerDelegate to the declaration file (i.e. your .h file)

Step 1: just install the gestureRecognizer delegate: (in the .m file)

 UITapGestureRecognizer *webViewTapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; webViewTapped.numberOfTapsRequired = 1; webViewTapped.delegate = self; [webView addGestureRecognizer:webViewTapped]; [webViewTapped release]; 

Step 2: override this function: (in .m file)

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } 

Step 3: Now we implement the tapAction function:

 - (void)tapAction:(UITapGestureRecognizer *)sender { CGPoint point = [sender locationInView:self.view]; // get x and y from here } 
+2
source

Source: https://habr.com/ru/post/951640/


All Articles