Re: Group CGAffineTransform Animations?
Re: Group CGAffineTransform Animations?
- Subject: Re: Group CGAffineTransform Animations?
- From: Graham Cox <email@hidden>
- Date: Thu, 18 Jun 2009 00:37:32 +1000
On 18/06/2009, at 12:21 AM, Chunk 1978 wrote:
premiss: a red cube, 50 width x 50 height, located at {0,0}. i want
to scale the cute 2 times it's width and height, move it to the center
of the screen, and rotate it 90ยบ
-=-=-=-
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
CGRect screen = [[UIScreen mainScreen] bounds];
CGPoint centerPoint = {CGRectGetMidX(screen), CGRectGetMidY(screen)};
CGAffineTransform transform = CGAffineTransformMakeScale(2, 2);
transform = CGAffineTransformTranslate(transform, centerPoint.x,
centerPoint.y);
transform = CGAffineTransformRotate(transform, kDegreesToRadian(90));
square.transform = transform;
[UIView commitAnimations];
-=-=-=-
so, because of this matrix multiplication (that is way over my head,
i'm not a math person), the above code moves the cube all the way the
the bottom right of the screen (but not to the very bottom right,
which confuses me). however, if i don't scale the cube, leaving it at
CGAffineTransformMakeScale(1, 1), the translation works and the cube
goes to the center of the screen (but of course not i have no
scale)...
if the above code moved the cube to the very bottom right, i would
just assume that the Translate is being multiplied by the Scale, and
go with that, but it's not that simple, and it's kinda making me
crazy... is there a formula i can use? or does multiplying
CGAffineTransform matrices generally warrant no control?
Of course you have complete control. The problem is that operations
have to be performed in the right order.
It's fair to say that transforms can be a bit unintuitive - you expect
them to perform the operations in the order you set. In fact, the
reverse order is what it will actually do. (The explanation for this
lies in the maths, but it sounds like you're not interested in that,
in which case this might be a struggle).
Because all transformations take place relative to the origin (0,0),
you generally need to ensure that the translation step, which moves it
away from the origin, is performed last. This means it should be the
FIRST operation listed in your code. The order of rotations and
scaling is less critical.
So try starting with the translation, then scale, then rotate and see
if it works. I find it helps to just read the list of operations in
reverse, then I can "see" what it will do. In your case it will first
rotate the shape (OK), then translate it to the centre of the screen,
then scale it *relative to the origin*, which is now a long way away,
so the object will end up scale * distance away, which is not where
you wanted it.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden