In this Situation you should check first check wether there Rectangles are overlapped or not.
To Check Wether Overlapped or not
CGRect RectA = CGRectMake(50, 50, 50, 50);
CGRect RectB = CGRectMake(100, 100, 50, 50);
if (CGRectGetMinX(RectA) < CGRectGetMaxX(RectB) && CGRectGetMaxX(RectA) > CGRectGetMinX(RectB) &&
CGRectGetMinY(RectA) < CGRectGetMaxY(RectB) && CGRectGetMaxY(RectA) > CGRectGetMinY(RectB) )
{
NSLog(@"overlapped");
}
else
{
NSLog(@"Not overlapped");
}
after finding OverLapped or not Find Centres of Both the Rectangles and Do whatever You Want.
To Find Center.
CGPoint centerB = CGPointMake((CGRectGetMinX(RectB) + CGRectGetMaxX(RectB))/2, (CGRectGetMinY(RectB) + CGRectGetMaxY(RectB))/2);
CGPoint centerA = CGPointMake((CGRectGetMinX(RectA) + CGRectGetMaxX(RectA))/2, (CGRectGetMinY(RectA) + CGRectGetMaxY(RectA))/2);