【问题标题】:how to find the center/x,y coordinate of an image in UIWebView如何在 UIWebView 中找到图像的中心/x,y 坐标
【发布时间】: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


【解决方案1】:

如果您在 html 页面中使用 jQuery,那么您可以尝试使用:

NSString * offsetTop = [NSString stringWithFormat:@"$(document).elementFromPoint(%f, %f).offsetTop;", pt.x, pt.y
                      ];

还要注意语句末尾的分号。所有 Javascript 语句都必须有效且正确终止,就像在 html 页面中一样。

如果你是注入一个库,你必须正确地放入一个带有命名空间的函数类中,如下:

"(function () {return "your data";}) ();"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多