0

I have a UIButton and a UIView that belong to two different views hierarchies and I'm trying to detect a collision when I drag my button to the “viewTrashArea”.

The problem is that the frame of the button and the frame of the view are in different coordinates and therefore make the collision think that they are touching but they are far away from each others.

How can I detect the collision based on the global screen position?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
mhamdouchi
  • 21
  • 6
  • 1
    http://stackoverflow.com/questions/8465659/understand-convertrecttoview-convertrectfromview-convertpointtoview-and – mattsven Dec 05 '13 at 18:51

2 Answers2

0

You'll need to convert the positions to a common coordinate system. Use the UIView methods convertRect:fromView: and convertRect:toView: for this purpose.

0

Given

UIButton *button;
UIView *viewTrashArea;

This line would return true if they intersect:

CGRectIntersectsRect([button convertRect:button.bounds toView:viewTrashArea], viewTrashArea.bounds);
matehat
  • 5,214
  • 2
  • 29
  • 40