在
代码变成如下所示:
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
...