Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect whenever two UIImageView overlap?

I have two UIImageViews, one of them is moving from left to right, the other one is touch draggable. iI want NSLog to show up a message on console whenever imagetwo overlaps imageone. How Do I do this?

like image 345
John Avatar asked Feb 08 '11 05:02

John


2 Answers

You can use the CGRectIntersectsRect function to easily test rectangle intersection, provided the UIImageViews share the same superview (more exactly, have the same coordinate space).

Likely you will need to add code like the following:

  -(void) touchesEnded:(NSSet *) touches {
    if(CGRectIntersectsRect([imageViewA frame], [imageViewB frame])) {
      NSLog(@"Do something.");
    }
  }

to the UIView that hosts both image views, or a similar method that is called whenever the drag is completed.

like image 189
Chris Zelenak Avatar answered Oct 15 '22 22:10

Chris Zelenak


try something like this.

if (CGRectContainsRect([myImageView1 frame], [myImageView2 frame])) {
        NSLog(@"Overlaped, it's working!");
}
like image 1
Rajender Kumar Avatar answered Oct 15 '22 21:10

Rajender Kumar