检测UIView是否与其他UIView相交 [英] Detecting if UIView is intersecting other UIViews
问题描述
我在屏幕上有一堆UIViews。我想知道什么是最好的方式来检查一个特定的视图(我已经参考)是交叉ANY其他视图。我现在做的方式是,迭代所有的子视图,并检查一个一个,如果有一个框架之间的交集。
I have a bunch of UIViews on the screen. I would like to know what's the best to way to check if a particular view (which I have reference to) is intersection ANY other views. The way I am doing it right now is, iterating though all the subviews and check one by one if there's an intersection between the frames.
这看起来效率不高。有没有更好的方法来做这个?
This doesn't seem very efficient. Is there a better way to do this?
推荐答案
首先,创建一个数组,存储所有的UIViews的框架及其相关联的引用。
First, create some array that stores the frames of all of your UIViews and their associated reference.
然后,可能在后台线程中,您可以使用数组中的内容运行一些冲突测试。对于一些简单的冲突测试只有矩形,检查这个问题:矩形的简单碰撞算法
Then, potentially in a background thread, you can run some collision testing using what's in the array. For some simple collision testing for rectangles only, check out this SO question: Simple Collision Algorithm for Rectangles
希望有帮助!
这篇关于检测UIView是否与其他UIView相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!