1
votes

I have searched stack overflow too many times and also posted a question but in vain. I need to implement a function in my collage app where user can rotate a uiimage and move it as rotated image. Any idea of how to do it? I will try all of them if needed. Am being frustrated searching for it for last two days.

At first i have rotated the frame of the UIImage using CGAffineTransformMakeRotation(angle). It was working but when moving the image after rotation, it was creating distortion. So any other way of doing it?

edit: to make it clear, rotation angle is custom.

2
You posted some code for rotating, but how did you move the view ? More code is needed to understand what you did.A-Live
@Maniac One: You have distortion if rotation angle equal 90*n degrees? If distortion appear only if angle not equal 90*n degrees - you move frame by change origin of frame?Ruslan Soldatenko
@A-Live i have shown there how i have moved the view. i have done it by updating the imageView in an array and performing the same 'handlePanGesture' function.Maniac One
@RuslanSoldatenko Distortion is happening all the time. I am rotating it using UIRotationGestureRecognizer. On rotation everything is fine. When am moving the view after rotation its distorting. Ya am using change of origin of frame :Maniac One
@Maniac One: I came across a distortion when picture was moved. It happened with pictures turned through an angle not equal to 90*N degrees. The distortions were, if I moved the image by changing the coordinates of the frame. If I changed the coordinates of the center - there was no distortion.Ruslan Soldatenko

2 Answers

5
votes

Having examined some of the similar questions (Change uiview size after rotation transform and How to resize the UIView when CGAffineTransformIdentity) and read the Official documentation about CGAffineTransform I have come to some simple conclusions. Will explain them below.

When you use CGAffineTransform for some object with followed frame transform you must use some rules for obtain correct result:

  1. If transform property of object is equal CGAffineTransformIdentity you can change frame of object or use CGAffineTransform without restriction.

  2. If If transform property of object is not equal CGAffineTransformIdentity and you want change frame of object without CGAffineTransform:
    a) save value of object transform to some local (or another type) variable
    b) set transform property of object to CGAffineTransformIdentity
    c) change frame of object
    d) restore transform value from local (or another type) variable.

  3. If transform property of object is not equal CGAffineTransformIdentity and you want use CGAffineTransform, for example new_transform:
    a) use CGAffineTransformConcat([object transform] , new_transform) to obtain result_transform
    b) set transform value of object to result_transform

Compliance with these simple rules will help avoid many problems.

Note: you must remember, when you use CGAffineTransformConcat all transform totalized. For example: if you want rotate object from 6 degree to 7 degree, you must add transform rotate to 1 degree, not to 7. Otherwise you obtain rotation 6 + 7 = 13 degree.

1
votes

For anyOne who may migrate here may find this part of the Code useful:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
imageToBeMoved.center = CGPointMake(primaryTapPoint.x   ,primaryTapPoint.y); 
[UIView commitAnimations]; 

Changing: imageToBeMoved.frame to imageToBeMoved.center solves the problem