views:

29

answers:

1

I have a UIView as the container/holder view. Inside that i have a group of UIViews (icons)

Think of this as similar to the iPhone's SpringBoard- you have an SBIconList (UIView subclass) with a group of SBIcons (also UIView subclasses)

So far, each icon of my icons is draggable, but i'm having problems writing the collision detection code to detect a hit between the dragging icon and another icon.

My basic setup so far is.. i'm writing the hit-test code under the scope of the icon's touchesMoved event. I then 'for' loop through every subview of the container view (each icon) and try a 'CGRectContaninsPoint(currentSubviewFrame, touchPoint)'. currentSubviewFrame being the frame for the icon in the current loop of the 'for' loop. And touchPoint being the x/y of the touch point on screen.

The problem is that because the code is written under the scope of one of the container subviews, the coordinates seem to become relative to the subview. For example, the coordinate of the touchPoint (globally) should be, say.. 160,240 (center screen) but this coordinate is NSLog'ed as, say.. 30,30. (Central relative to the 60x60 icon) Same case for the currentSubviewFrame x, y, width & height.

I should mention that this code HAS to written under the scope of the subview's touchesMoved event.

Any help would be appreciated. Thanks in advance:)

A: 

Take a look at theses functions for converting coordinates between views:

- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view

You could use -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event on all your subviews (except the one you're dragging) to find out if there was a collision.

aegzorz
Ah, exactly what i need! Thank you! :)
Jamie
Great! If that answered your question please mark the answer as accepted.
aegzorz