【问题标题】:How to check whether a CGPoint is inside a UIImageView?如何检查 CGPoint 是否在 UIImageView 内?
【发布时间】:2010-06-02 14:47:47
【问题描述】:

touchesBegan:

CGPoint touch_point = [[touches anyObject] locationInView:self.view];

大约有几十个UIImageView,存储在一个NSMutableArrayimages中。我想知道是否有一个内置函数来检查 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]) 

结果是一样的。从不返回 true。

为了消除任何与图像有关的可能性。我尝试了以下方法:

  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 总是返回false ...

【问题讨论】:

  • 删除“touch_point = [touch locationInView:self.view];”。并添加“for (UIImageView *image in piece_images) { ... }”:“touch_point = [touch locationInView:image];”。这就是诀窍:)!

标签: iphone


【解决方案1】:

在我看来问题是您需要将坐标从视图坐标转换为UIImageView 的坐标。你可以使用UIViewconvertPoint方法。

尝试替换

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

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

(请注意,您可以通过nil 而不是event。)

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

【讨论】:

    【解决方案2】:
    if ([image pointInside:touch_point withEvent:event]) {
        // Point inside
    }else {
        // Point isn't inside
    }
    

    在这种情况下,事件取自:

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

    【讨论】:

    • 当以编程方式调用时,您可以将nil 传递给event
    【解决方案3】:
    - (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 上都试过

    【讨论】:

    • 我正要写这个,但我看到你自己想出来了。我认为这是最好的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    相关资源
    最近更新 更多