Search code examples
iosuiimageviewintersectioncgrect

CGRect Intersects Rect better alternative


I'm trying to want to write an method whenever two UIImageViews intersect. The only way I know how to do it is CGRectIntersectsRect. But that only works with rectangles, but my images are circles. Isn't there a better alternative? Thank you!


Solution

  • You can do something like this:

    CGPoint a = imageViewA.center;
    CGPoint b = imageViewB.center;
    CGFloat distance = sqrt(pow((b.x - a.x),2) + pow((b.y - a.y),2));
    
    if(distance < (imageViewA.bounds.size.width/2.0 + imageViewB.bounds.size.width/2.0)){
        //images are touching.
    }