All UIViews have a transform property that you can set, that will be applied to that view.
UIView *myView = [[UIView alloc] init];
myView.transform = CGAffineTransformIdentity;
Here I set the transformation matrix of the view to the Identity matrix.
The CoreGraphics API on the iPhone has a bunch of functions that will create a new transformation matrix and let you apply those transformations to your view. These are things like:
CGAffineTransformMakeScale, CGAffineTransformMakeRotation, CGAffineTransformMakeTranslation, etc
You use these, when you are creating an initial transformation matrix for a particular view. So in my code, I have a view that I want to rotate. The code will look like this:
UIView *myView = [[UIView alloc] init];
myView.transform = CGAffineTransformMakeRotation(M_PI);
Later on, if I want to apply more transformations to the UIView, I will use:
CGAffineTransformScale, CGAffineTransformRotate, CGAffineTransformTranslate, etc
Notice that these do not have "Make", because we are not creating a new transformation matrix, but instead we are operating on an existing transformation matrix. So now, I can translate my UIView with this:
myView.transform = CGAffineTransformTranslate(myView.transform, 25, 75);
To animate these transforms, I can put them in an animation block:
[UIView beginAnimations: @"rotate" context: nil];
[UIView setAnimationDuration: 2.0];
myView.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];
This will rotate my view 180 degrees over 2 seconds.
If you don't use the correct functions, you will likely see some strange behavior and spend a lot of time wondering why things aren't working. This is what happened to me. In my particular case, I was using a "CATransform3DMakeTranslation" where I should have been using a "CATransform3DTranslate".
Once iPhone OS 4 comes out, I will have to redo the animations in my code, because, according to the documentation, "Use of this method is discouraged in iPhone OS 4.0 and later. You should use the block-based animation methods instead".
No comments:
Post a Comment