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.
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:
If transform property of object is equal CGAffineTransformIdentity
you can change frame of object or use CGAffineTransform
without restriction.
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.
- 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.
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