I have an array of 16 boxViews, which are present in random locations on my superview. I am dragging myView which is a special subView. I need to be able to tell which boxView(s), myView is intersecting with, at any given time. How do I do achieve that?
Asked
Active
Viewed 874 times
0
-
What have you tried that doesn't work? You have tagged `CGRectIntersectsRect`, so you obviously know about all of the pieces... – Ian MacDonald Feb 18 '15 at 19:28
-
@Ian MacDonald, I am able to use CGRectIntersectsRect to test, if myView is intersecting with two (or more) boxViews, but I need to know the name(array position) of the boxViews I am testing against. Now it dawns upon me, that I can use a loop to test against each member of the array to tell which array members myView is intersecting with. Thank you very much. Would this be the best way to achieve this? – Kashif Feb 18 '15 at 19:32
1 Answers
0
Suppose you have boxViews
that is an array of all the view in your super view. And all UIViews
in boxViews
have the same immediate super view. And that myView
is among the views in boxViews
.
let intersectedViews = boxViews.filter { $0 !== myView }
.filter { CGRectIntersection(myView.frame, $0.frame) }

yusuke024
- 2,189
- 19
- 12
-
Slightly cleaner: `let intersectingViews = boxViews.filter { $0 != myView && myView.frame.intersects($0.frame) }` – Jawwad Feb 18 '15 at 23:35
-
I didn't test this solution because I went with going through my array of boxViews and test each one of them for intersection against myView. It worked well, thanks to @Ian MacDonald for pointer. – Kashif Feb 22 '15 at 14:11