Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将UIImageView与另一个UIImageView关联_Ios_Xcode - Fatal编程技术网

Ios 将UIImageView与另一个UIImageView关联

Ios 将UIImageView与另一个UIImageView关联,ios,xcode,Ios,Xcode,我在屏幕上有10个UIImageView。它们都在一个名为posArray的数组中。我还有另一个UIImageView,由用户拖动,可以点击其他10个。如果一个对象击中其他10个对象中的任何一个,那么显示简单NSlog消息的简单方法是什么 现在我正在使用touchesBegin,touchesMoved移动我的一个对象和下面的这个数组来测试一个对象是否碰到了其他十个对象中的任何一个 我只是在想,有一种更简单、内存消耗更少的方法,出于某种原因,可以做到这一点 for (int i = 0; i &

我在屏幕上有10个UIImageView。它们都在一个名为posArray的数组中。我还有另一个UIImageView,由用户拖动,可以点击其他10个。如果一个对象击中其他10个对象中的任何一个,那么显示简单NSlog消息的简单方法是什么

现在我正在使用touchesBegin,touchesMoved移动我的一个对象和下面的这个数组来测试一个对象是否碰到了其他十个对象中的任何一个

我只是在想,有一种更简单、内存消耗更少的方法,出于某种原因,可以做到这一点

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的两边都是一样的?第二个不应该在那里。