objective-c - 检查两个或多个 CGRects 在 NSMutableArray 中时是否接触的好方法是什么?

标签 objective-c ios xcode uiimageview intersection

我在 NSMutableArray 中有几个 UIImageView。它们都在 superView 中。我想检查 UIImageView 是否与数组中的另一个 UIImageView 相交(碰撞、碰撞或接触)。这是我一直在做的,但我认为应该有更好的方法。

for (UIImageView *b in _blocks) {
    for (UIImageView *b2 in _blocks) {
        if (CGRectContainsPoint(b2.frame, CGPointMake(b.center.x, b.center.y + b.frame.size.height/2))) {
            // Do something
        }

    }

}

如果我想检查 ImageView 是否以这种方式接触了 2 个以上的 View ,代码将变得非常糟糕。这就是为什么我想知道更好的方法。

最佳答案

这样做:

 NSMutableArray *arrIntersect = [NSMutableArray array];
 for (UIImageView *b in _blocks) {
    for (UIImageView *b2 in _blocks) {
         if (b != b2 && CGRectIntersectsRect(b2.frame, b.frame)) {
            if(arrIntersect.count > 2)
               //count > 2
            else
               [arrIntersect addObject:b2];
        }
    }
}

关于objective-c - 检查两个或多个 CGRects 在 NSMutableArray 中时是否接触的好方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12799961/

相关文章:

ios - 创建编辑按钮

ios - 如何删除默认按钮?

ios - cellForRow At IndexPath 执行一次太少?

Xcode:确保标题注释中的文件名正确?

ios - 在 ios 中将图像转换为卡通

ios - UITableView ContentInsets 水平滚动内容

ios - 在 uitableview 上添加 uibutton

iphone - 创建对象 - Objective-C

ios - SDK版本10的功能可以在系统版本8的设备上运行吗?

iphone - iOS 谷歌地图问题