In 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?
}
Thanks
跟进:
由于未知的原因,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
output.
我也尝试过:
if ([image pointInside:touch_point withEvent:nil])
结果是一样的。永远不会返回 true。
消除任何与图像相关的可能性。我尝试了以下方法:
if (YES or [image pointInside:touch_point withEvent:event])
第一次点击屏幕后,所有图像都会隐藏。
EDIT 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
...