Ios 将UIImageView与另一个UIImageView关联
我在屏幕上有10个UIImageView。它们都在一个名为posArray的数组中。我还有另一个UIImageView,由用户拖动,可以点击其他10个。如果一个对象击中其他10个对象中的任何一个,那么显示简单NSlog消息的简单方法是什么 现在我正在使用touchesBegin,touchesMoved移动我的一个对象和下面的这个数组来测试一个对象是否碰到了其他十个对象中的任何一个 我只是在想,有一种更简单、内存消耗更少的方法,出于某种原因,可以做到这一点Ios 将UIImageView与另一个UIImageView关联,ios,xcode,Ios,Xcode,我在屏幕上有10个UIImageView。它们都在一个名为posArray的数组中。我还有另一个UIImageView,由用户拖动,可以点击其他10个。如果一个对象击中其他10个对象中的任何一个,那么显示简单NSlog消息的简单方法是什么 现在我正在使用touchesBegin,touchesMoved移动我的一个对象和下面的这个数组来测试一个对象是否碰到了其他十个对象中的任何一个 我只是在想,有一种更简单、内存消耗更少的方法,出于某种原因,可以做到这一点 for (int i = 0; i &
for (int i = 0; i < [posArray count]; i++) {
UIImageView *tempPos;
tempPos = [posArray objectAtIndex:i];
if (CGRectIntersectsRect(red1.frame, tempPos.frame)) {
red1.center = CGPointMake(tempPos.center.x, tempPos.center.y);
NSLog(@"position piece touched");
}
}
您还可以使用快速枚举来提高速度。如果只需要一个匹配项,也可以在找到一个匹配项后添加中断语句:
for (UIImageView * tempPos in posArray){
if (CGRectIntersectsRect(red1.frame, tempPos.frame)) {
red1.center = CGPointMake(tempPos.center.x, tempPos.center.y);
NSLog(@"position piece touched");
break;
}
}
在你的if条件下,OR的两边都是一样的?第二个不应该在那里。