Quote:
Originally Posted by Sosumi
Hey, I'm trying to move a UIImageView using CGAffineTransform.
I need the UIImageView to move at a specific angle (in radians).
Does anyone know how to do this?
Thanks.
|
It's not clear what you are asking.
Are you asking to shift a view controller's position as if it was moving away from it's center point on a particular compass heading?
Or are you asking how to rotate an image view in place?
Or are you asking something completely different.
If you're asking about rotating an image view in place, the other poster gave you the code you need.
If you want to know how to shift an object away from it's center point as if it was moving in a compass direction, you need to use trig to calculate the new location:
Code:
//CGFloat r = radius of movement.
//CGFloat theta = angle to move, in radians
CGFloat deltaX = r * cosf(theta);
CGFloat deltaY = r * sinf(theta);
CGPoint viewCenter = myImageView.center;
viewCenter.x += deltaX;
viewCenter.y += deltaY;
myImageView.center = viewCenter.x;
EDIT: That code moves the object by changing it's center, which is better than changing the transform because you can change the center like that even if you're previously applied rotation or scaling to the object's transform. If you use the view's transform to shift it's position, the results will be different depending on whether or not the transformation matrix is currently the identity matrix. If the view's transform has been rotated or scaled, those changes will probably cause further changes in the view transform to do something you didn't intend. (order of operations matters a great deal with transformation matrixes.)
You could also do the calculations for deltaX and deltaY as described above, and then apply them to your view's transform using the CGAffineTransformTranslate function.