问题
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.
回答1:
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 useCGAffineTransform
without restriction.If If transform property of object is not equal
CGAffineTransformIdentity
and you want change frame of object withoutCGAffineTransform
:
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 useCGAffineTransform
, for examplenew_transform
:
a) useCGAffineTransformConcat([object transform] , new_transform)
to obtainresult_transform
b) set transform value of object toresult_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.
回答2:
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
来源:https://stackoverflow.com/questions/19837512/rotate-uiimage-and-move