如何检查一个 CGPoint 是否在 UIImageView 内部?

4
touchesBegan:方法中,
CGPoint touch_point = [[touches anyObject] locationInView:self.view];

周围有数十个UIImageView,存储在一个NSMutableArray images中。我想知道是否有内置函数来检查一个CGPoint(touch_point)是否在其中一张图片内,例如:

for (UIImageView *image in images) {
   // how to test if touch_point is tapped on a image?
}

谢谢

跟进:

由于未知原因,pointInside 从未返回 true。以下是完整代码。

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
      UITouch *touch = [touches anyObject]; 
        touch_point = [touch locationInView:self.view];
        for (UIImageView *image in piece_images) {
            if ([image pointInside:touch_point withEvent:event]) {
                image.hidden = YES;
            } else {
                image.hidden = NO;
            }
            NSLog(@"image %.0f %.0f touch %.0f %.0f", image.center.x, image.center.y, touch_point.x, touch_point.y);
        }
    } 

虽然在NSLog输出中有时我能看到这两个点是相同的。

我也尝试过:

  if ([image pointInside:touch_point withEvent:nil]) 

结果是一样的,从未返回真。

为了消除任何与图像有关的问题,我尝试了以下方法:

  if (YES or [image pointInside:touch_point withEvent:event])

第一次点击屏幕后,所有图像都会隐藏。

编辑2:

真的很奇怪。即使我硬编码了这个:

        point.x = image.center.x;
        point.y = image.center.y;

代码变成如下所示:
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
        UITouch *touch = [touches anyObject]; 
        CGPoint point; // = [touch locationInView:self.view];
        for (UIImageView *image in piece_images) {
            point.x = image.center.x;
            point.y = image.center.y;       
            if ([image pointInside:point withEvent:event]) {
                image.hidden = YES;
                NSLog(@"YES");
            } else {
                image.hidden = NO;
                NSLog(@"NO");
            }
            NSLog(@"image %.0f %.0f touch %.0f %.0f", image.center.x, image.center.y, point.x, point.y);
        }
    }

pointInside always returns false ...


删除 "touch_point = [touch locationInView:self.view];"。并在 "for (UIImageView *image in piece_images) { ... }" 中添加 "touch_point = [touch locationInView:image];"。这样就可以解决问题了 :)! - cutsoy
3个回答

12

听起来问题是您需要将坐标从您的视图坐标系转换为 UIImageView 的坐标系。您可以使用 UIViewconvertPoint 方法。

尝试替换

if ([image pointInside:touch_point withEvent:event]) {

使用

if ([image pointInside: [self.view convertPoint:touch_point toView: image] withEvent:event]) {

(请注意,您可以传递 nil 而不是 event 。)

我想这可能对提问者来说有点晚了,但我希望对某人有用。


0
if ([image pointInside:touch_point withEvent:event]) {
    // Point inside
}else {
    // Point isn't inside
}

在这种情况下,事件是从以下内容中获取的:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

在程序中调用时,可以将 nil 传递给 event - kennytm

0
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];

    for (UIImageView *piece in piece_images) {
        CGFloat x_min = piece.center.x - (piece.bounds.size.width / 2);
        CGFloat x_max = x_min + piece.bounds.size.width;
        CGFloat y_min = piece.center.y - (piece.bounds.size.height / 2);
        CGFloat y_max = y_min + piece.bounds.size.height;
        if (point.x > x_min && point.x < x_max && point.y > y_min && point.y < y_max ) {
            piece.hidden = YES;
        } else {
            piece.hidden = NO;
        }
    }

}

太糟糕了,我必须自己做...

这是OS 3.2,XCode 3.2.2,在模拟器和iPad上都尝试过。


我正要写这个,但是我看你自己已经想出来了。我认为这是最好的方法。 - Brock Woolf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接