Rotate UIImage and Move

这一生的挚爱 提交于 2020-01-06 03:18:07

问题


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:

  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.




回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!