I animate a lot of views using (..) [UIView commitAnimations].
Now i want to check if the view, which was "shot", has touched another view.
Does anyone know how to implement this?
Maybe if(view.frame.origin.x==anotherview.frame.origin.x){
} ..
Conceptually, a view "touches" another view if their bounding rects intersect. So to compare the bounding rects of two views, you want to do something like:
Boolean viewsOverlap = CGRectIntersectsRect(viewA.bounds, viewB.bounds);
But that alone won't work because the bounding rects of the views are specified in their own coordinate spaces (meaning both start at 0,0, etc. etc.) So you also need to transform the rects to a common coordinate space before you compare them:
CGRect boundsA = [viewA convertRect:viewA.bounds toView:nil];
CGRect boundsB = [viewB convertRect:viewB.bounds toView:nil];
Boolean viewsOverlap = CGRectIntersectsRect(boundsA, boundsB);
From there, you should be able to figure out how to iterate efficiently through your list of views-you-care-about to determine if any overlap.
Alternatively you could just compare the frames if they're in the same superview:
BOOL methodB = CGRectIntersectsRect(viewA.frame, viewB.frame);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With