Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView, Zoom & elementFromPoint

In a UIWebView I need to access properties of the DOM element (from a SVG graph) when I longTap on it. To do so, I've added a UILongPressGestureRecognizer as following :

UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action: @selector(longPress:)];
[self.webView addGestureRecognizer: longPress];

When I longPress on the view, the handler get called from which I call a JS function :

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

This is my JS handler :

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

However it appears that UIWebView coordinates are != from the DOM coordinates (where I click does not correspond to the localName shown in the alert). I've managed to figure out that there is +/- a 1.4 factor between UIWebView coordinates & JS ones (by clicking in the lower-right hand side of the screen and comparing these values with window.innder{Width,Height}.

My guess was that UIWebView might apply a default Zoom ratio initially, but I can't find what this values corresponds to.

Moreover, I'll also need a way to make this work when the user actually zooms/moves the page.

Does anybody knows what I'm doing wrong ?

Thanks,

like image 389
Cuva Avatar asked Dec 05 '25 06:12

Cuva


1 Answers

Okay, I finally found what's the problem.

It was coming from the zoom ratio, and here is how I managed to fix it :

- (void) longPress: (UIGestureRecognizer *) gesture {
    int displayWidth = [[self.webView stringByEvaluatingJavaScriptFromString:@"window.innerWidth"] intValue];
    CGFloat scale = self.webView.frame.size.width / displayWidth;

    CGPoint curCoords = [gesture locationInView:self.webView];

    curCoords.x /= scale;
    curCoords.y /= scale;

    if (!CGPointEqualToPoint(curCoords, self.lastLongPress)) {
        self.lastLongPress = curCoords;

        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"longPress(%f, %f)", curCoords.x, curCoords.y]];
    }
}

And the JS Handler :

function longPress(x, y) {
    var e = svgDocument.elementFromPoint(x, y);

    alert('Youhouu ' + e.localName);
}

It appears that it is not needed to add the pageOffset as now UIWebView automatically adds it (from iOS 5 I believe).

Cheers,

like image 101
Cuva Avatar answered Dec 07 '25 21:12

Cuva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!