How do I detect when two UIImageView overlap?

I have two UIImageViews, one of which moves from left to right, and the other is draggable. I want NSLog to display a message on the console whenever imagetwo overlaps imageone. How to do it?

+5
source share
4 answers

You can use the function CGRectIntersectsRectto easily check the intersection of rectangles if UIImageViews have the same supervisor (more precisely, have the same coordinate space).

You will probably need to add the code as follows:

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

UIView, , , , .

+11

- .

if (CGRectContainsRect([myImageView1 frame], [myImageView2 frame])) {
        NSLog(@"Overlaped, it working!");
}
+1

You can use:

CGRectIsNull (CGRectIntersection (view1.bounds, view2.bounds));

0
source

Swift 3.0 has become ...

if (self.image2P.bounds.contains(self.image3P.bounds)) {
    print("Overlaped, it working!")
}
0
source

All Articles