检测 UIView 是否与其他 UIView 相交 [英] Detecting if UIView is intersecting other UIViews
问题描述
我在屏幕上有一堆 UIView.我想知道检查特定视图(我所参考的)是否与任何其他视图相交的最佳方法是什么.我现在这样做的方式是,遍历所有子视图并逐个检查帧之间是否存在交集.
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?
推荐答案
首先,创建一些数组来存储所有 UIView 的框架及其相关引用.
First, create some array that stores the frames of all of your UIViews and their associated reference.
然后,可能在后台线程中,您可以使用数组中的内容运行一些碰撞测试.对于仅针对矩形的一些简单碰撞测试,请查看此 SO 问题:矩形的简单碰撞算法
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屋!