6

I have a UIWebView that displays a generated html table. When the user taps on a cell in the html table, my app needs to know which cell they've tapped, and the (x,y) coordinate of the tap location so I can display a popover at that point.

I've implemented shouldStartLoadWithRequest in my UIWebView delegate. In my web page, I've embedded javascript code that captures the touch event and passes what should be the (x,y) coordinate of the touched point in a 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 gets an onclick event that calls cellTapped():

<td onclick="cellTapped(event)">...</td>

So whenever the user touches anywhere in the UIWebView, I get the coordinate of the touch point, which I save off in x and y. If they touch within one of the table cells, I receive the touch event (which sets x and y), then cellTapped() gets called and I set window.location.href, passing the (x,y) coordinate into my app.

This all works beautifully. Unless the user has zoomed or scrolled the UIWebView. When they zoom or scroll, the x and y coordinates I'm getting from event.touches[0].clientX and event.touches[0].clientY are off by some varying number of pixels (varies with the amount of zoom and how far up/down or left/right the web view is scrolled).

Is there some way to determine the zoom ratio and scroll position of the web view so that I can adjust my x and y coordinates accordingly? The zoomScale and contentOffset properties from UIScrollView do not seem to be exposed in UIWebView.

4

2 回答 2

2

使用 UIGestureRecognizerDelegate 方法:

在声明文件(即您的 .h 文件)中添加 UIGestureRecognizerDelegate

第1步:只需设置gestureRecognizer的委托:(在.m文件中)

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

第 2 步:覆盖此函数:(在 .m 文件中)

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

第 3 步:现在实现 tapAction 函数:

- (void)tapAction:(UITapGestureRecognizer *)sender
{    
    CGPoint point = [sender locationInView:self.view]; // get x and y from here
}
于 2013-08-13T14:16:45.933 回答
1

编辑:在 iOS 5 及更高版本中, UIWebView 的 scrollView 属性已公开且可访问,因此这不是问题。就我而言,我仍然需要支持运行 iOS 4 的设备(信不信由你……),因此以下解决了旧版本的问题。

通过循环 my 的子视图UIWebView,我可以找到底层UIScrollView,然后使用它的zoomScalecontentOffset属性来找到缩放和滚动位置:

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;
    }
}

我不知道 Apple 是否会批准此应用商店提交,因为我认为他们有理由不公开底层UIScrollView对象,但它确实解决了我的问题。无论如何,我的应用程序是根据企业许可证分发的,因此应用程序商店提交对我来说不是问题。

于 2014-05-08T13:53:10.853 回答