How can I determine if two uiviewimages intersect. I want to do an auto snap feature. When the small image intersects with the big image or close to it (Lets says the distance<=x), I want the small image automatically snap(connect) to the big image at the point where they intersect.
3 Answers
CGRect bigframe = CGRectInset(bigView.frame, -padding.x, -padding.y);
BOOL isIntersecting = CGRectIntersectsRect(smallView.frame, bigFrame);

- 366
- 2
- 7
-
what are padding.x and padding.y? Sorry I am a newbie :) – Asım Sinan Yüksel Jun 01 '12 at 18:12
-
You only really need the second bit but you could inset by -padding to make a bigger rect for the smaller view to snap into the bigger one when it is not quite intersecting the view itself. You could use the same value for x and y if you want or declare a cgpoint – Chris Heyes Jun 01 '12 at 19:38
You can use the CGRectIntersectsRect method to check for frames:
if (CGRectIntersectsRect(myImageView1.frame, myImageView2.frame))
{
NSLog(@"intersected")
}

- 14,323
- 10
- 62
- 102
The previous two posters have been on the right track with CGRectIntersectsRect
.
BOOL isIntersecting = CGRectIntersectsRect(smallImage.frame, largeImage.frame);
if(isIntersecting){
//Animate the Auto-Snap
[UIView beginAnimation:@"animation" context:nil];
[UIView setAnimationDuration:0.5];
smallImage.frame = largeImage.frame;
[UIView commitAnimations];
}
Basically what this says that if the two images are intersecting, the small image frame snap to the larger image over 0.5 seconds.
You don't have to animate it though; you could just make it instantaneous by removing all code except for smallImage.frame = largeImage.frame;
. However, I recommend the animation way.
Hope this helps.
-------EDIT--------
You could use the code:
BOOL isIntersecting = CGRectIntersectsRect(smallImage.frame, largeImage.frame);
if(isIntersecting){
//Animation
[UIView beginAnimation:@"animation" context:nil];
[UIView setAnimationDuration:0.5];
smallImage.center = CGPointMake(largeImage.center.x, largeImage.center.y);
//If you want to make it like a branch, you'll have to rotate the small image
smallImage.transform = CGAffineTransformMakeRotation(30);
//The 30 is the number of degrees to rotate. You can change that.
[UIView commitAnimations];
}
Hope this fixed your problem. Remember to up vote and pick as the answer if this helped.
-------EDIT---------
One last thing. I said that the "30" in CGAffineTransformMakeRotation(30)
stood for 30 degrees, but that is incorrect. The CGAffineTransformMakeRotation function takes its parameter in radians, so if you wanted 30 degrees you could do this:
#define PI 3.14159265
CGAffineTransformMakeRotation(PI/6);

- 11,515
- 7
- 53
- 92
-
Thanks for the reply.This works very well. Only problem is the small image goes inside the big image. I want something like this. Lets assume the small image is a branch of a tree. The big image is a big branch. When the small branch intersects with the big branch, the small branch's origin will connect to the surface of the big branch like in here http://postimage.org/image/u5790gt05/ . Should I use CGRectIntersection method? – Asım Sinan Yüksel Jun 02 '12 at 16:11
-
The CGRectIntersect method returns `YES` if they intersect and `NO` if they don't. I'll add more in my above post... – pasawaya Jun 02 '12 at 21:28