Distance between two rectangles
问题 How can I find the distance between two rectangles? Intersects should return 0 in distance. 回答1: Here's a quick function for calculating distance between two CGRects represented by a CGSize: CGSize CGSizeDistanceBetweenRects(CGRect rect1, CGRect rect2) { if (CGRectIntersectsRect(rect1, rect2)) { return CGSizeMake(0, 0); } CGRect mostLeft = rect1.origin.x < rect2.origin.x ? rect1 : rect2; CGRect mostRight = rect2.origin.x < rect1.origin.x ? rect1 : rect2; CGFloat xDifference = mostLeft.origin