【发布时间】:2012-01-15 17:06:46
【问题描述】:
所以我有一个 UIWebView,我在其中添加了一个触摸手势,我想找到在 web 视图中点击的图像的 x,y 原点坐标,或者如果可能的话,在 x y 坐标中找到图像的中心网络视图。我的代码是:
- (CGPoint) topLeftPointsForImage:(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];
NSLog(@"Coor height is %f with coor width is %f", x, y);
CGPoint point = CGPointMake(x, y);
return point;
}
但是,这似乎没有给我一个正确的值。我不是 javascript 大师,所以为此寻找一些建议。
【问题讨论】:
-
您获得了什么价值?您可能需要使用以下方法将 JS 代码中的结果转换为字符串:document.elementFromPoint(%f, %f).offsetTop.toString()
标签: javascript iphone objective-c ipad uiwebview