I have multiple UIImageView's
within a UIView
. These UIImageViews
have been transformed.
When the gesture is applied to any one of the UIImageView's
the overlapping imageViews should get displaced in such a way that the move outside the UIImageView's
frame and we get a full view of the image (all of this would be done in an animating manner).
Also please note that the images will have been transformed so I wont be able to use the frame properties.
Right now I'm using CGRectIntersectsRect
but its not giving me the desired effect.
After struggling a lot, finally i achieved the desired result with this algorithm.
I calculate the list of overlapping UIImageView's
and store it in an array.
I am using CGRectIntersectsRect
for this.
Note : This will also include the UIImageView
on which the gesture was applied (say target ImageView).
For each of these overlapping UIImageView, i perform the following steps :
1) Calculate the angle between the overlapping ImageView and target ImageView. This will give me the direction to shift the Overlapping ImageView.
2) Now shift the overlapping ImageView by some constant distance(say len), such that it maintains the same angle. Direction should be such that it moves away from the target ImageView.
3) You can calculate the new x,y co-ordinates using Sin/Cos functions.
x = start_x + len * cos(angle);
y = start_y + len * sin(angle);
NOTE: For ImageViews whose center is less than your target ImageView's center, you will need to subtract the value.
4) Now shift the overlapping ImageView to the new calculated center.
5) Continue shifting it until the views do not intersect any more.
I am attaching my code. I hope it helps.
-(void)moveImage:(UIImageView *)viewToMove fromView:(UIImageView *)imageToSendBack
{CGFloat angle = angleBetweenLines(viewToMove.center, imageToSendBack.center); CGFloat extraSpace = 50; CGRect oldBounds = viewToMove.bounds; CGPoint oldCenter = viewToMove.center; CGPoint shiftedCenter; while(CGRectIntersectsRect(viewToMove.frame,imageToSendBack.frame)) { CGPoint startPoint = viewToMove.center; shiftedCenter.x = startPoint.x - (extraSpace * cos(angle)); if(imageToSendBack.center.y < viewToMove.center.y) shiftedCenter.y = startPoint.y + extraSpace * sin(angle); else shiftedCenter.y = startPoint.y - extraSpace * sin(angle); viewToMove.center = shiftedCenter; } viewToMove.bounds = oldBounds; viewToMove.center = oldCenter; [self moveImageAnimationBegin:viewToMove toNewCenter:shiftedCenter]; }