如何检查CGPoint是否在UIImageView中?

时间:2010-06-02 14:47:47

标签: iphone

touchesBegan:

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

周围有数十UIImageViewNSMutableArray存储在images CGPoint中。我想知道是否有一个内置函数来检查其中一个图像中是否有for (UIImageView *image in images) { // how to test if touch_point is tapped on a image? } (touch_point),例如:

pointInside

由于

跟进:

由于未知原因, - (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); } } 永远不会返回true。这是完整的代码。

NSLog

虽然我可以看到 if ([image pointInside:touch_point withEvent:nil]) 输出中的两点有时相同。

我也尝试过:

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

结果是一样的。永远不会回归真实。

消除图像随之而来的可能性。我尝试了以下方法:

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

首次点击屏幕后隐藏所有图像。

编辑2:

真的很奇怪。即使我对此进行了硬编码:

    - (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始终返回{{1}} ...

3 个答案:

答案 0 :(得分:12)

对我而言,问题是您需要将坐标从视图的坐标转换为UIImageView的坐标。您可以使用UIView的{​​{1}}方法。

尝试替换

convertPoint

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

(请注意,您可以通过if ([image pointInside: [self.view convertPoint:touch_point toView: image] withEvent:event]) { 代替nil。)

我觉得这个问题有点晚了,但我希望它对某人有用。

答案 1 :(得分:0)

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

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

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

答案 2 :(得分: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上都尝试过