I have a UIWebView in which it has a UITapGestureRecognizer and basically in my tap gesture recognizer I have:
- (CGPoint) pointsForImage:(UIGestureRecognizer *)sender
{
CGPoint pt = [sender locationInView:self];
NSString * offsetTop = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).offsetTop", pt.x, pt.y
];
NSString * offsetLeft = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).offsetLeft", pt.x, pt.y
];
NSString * scrollTop = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).parentNode.scrollTop", pt.x, pt.y
];
NSString * scrollLeft = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).parentNode.scrollLeft", pt.x, pt.y
];
float x = [[self stringByEvaluatingJavaScriptFromString:offsetTop] floatValue] - [[self stringByEvaluatingJavaScriptFromString:scrollTop] floatValue];
float y = [[self stringByEvaluatingJavaScriptFromString:offsetLeft] floatValue] - [[self stringByEvaluatingJavaScriptFromString:scrollLeft] floatValue];
CGPoint point = CGPointMake(x, y);
return point;
}
basically what I am trying to do is find the top left x and y position of the image. Not relative to the UIWebView but to the content view. So say that the position of the image is 1000 pixels down, but I am now currently browsing the UIWebView down half way and therefore the number I want to see is 200. I hope this makes sense
Actually
UIWebViewhas aUIScrollView. You can access theUIScrollViewinUIWebViewusingscrollViewproperty. Then get thecontentOffsetof thescrollView. Finally, you got thecontentOffsetof thescrollViewand the image position relative to theUIWebView, you can calculate the point you want.Try this piece of Javascript:
stringByEvaluatingJavaScriptFromString below:
you’ll get the image position returned.