Search code examples
iosobjective-cuiviewframebounds

How do I detect how close UIView view is to the other UIView?


Is there a magic method that accepts two views and returns how close they are to one another (perhaps the x and y distances)? Or is this something that must be done manually?


Solution

  • Manually. As long as there is no transform applied to the views, it shouldn't be that hard to write some code that would calculate the distance between the 2 view's frame rectangles.

    Thinking out loud here:

    If you can't draw a vertical and horizontal line in the space between the 2 views frame rectangles, the distance will be the x distance or y distance between the nearest sides.

    If you can draw both a horizontal line and a vertical line between the views (they don't overlap in either the x dimension or the y dimension) then the distance between the 2 views will be the pythagorean distance between their nearest corners.