English 中文(简体)
我如何看待信箱中的形象 观点
原标题:how do I find the position of an image in a UIWebView

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

基本上,我正试图做的是找到图像的顶端左侧X和头寸。 并非与IDWebView有关,而是与内容观点有关。 因此,我说,图像的位置是1 000个缩影子,但我现在将“IDWeb”电文减半,因此我想看到的数字是200个。 我希望,这是有意义的。

最佳回答

Actually UIWebView has a UIScrollView. You can access the UIScrollView in UIWebView using scrollView property. Then get the contentOffset of the scrollView. Finally, you got the contentOffset of the scrollView and the image position relative to the UIWebView, you can calculate the point you want.

Try this piece of Javascript:

ThestringByEvaluatingJavaFromString below:

function getPosition(element){
    var el = element;
    var pos = [el.offsetLeft, el.offsetTop];
    var parent = el.offsetParent;
    if (parent != el) { 
      while (parent) {  
        pos[0] += parent.offsetLeft; 
        pos[1] += parent.offsetTop; 
        parent = parent.offsetParent;
      }
    }
    return pos;
}
getPosition(YourImageElement);

你们回去了图像位置。

问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签