Graphics Reference
In-Depth Information
LISTING 5-2
Continued
value = [NSValue valueWithCATransform3D:transform];
[animation setFromValue :value];
[animation setAutoreverses : YES ];
[animation setDuration :1.0f];
[animation setRepeatCount :100];
[workLayer addAnimation:animation forKey: kScaleKey ];
}
This method removes any existing animations, which keeps the animations from piling up
if the user clicks on several of the buttons. Next, we want to construct an animation object
that can instruct the layer on how to animate/transform. To do this, construct a
CABasicAnimation with a path of transform, which tells the animation that it will be modi-
fying the transform property of whatever layer it is applied to, and then start applying the
matrix transforms to the animation. You can use several methods to construct matrix trans-
forms. In this method, we use the CATransform3DMakeScale method and pass it the x -, y -,
and z -axis for the transform. As you can
see in Listing 5.2, we've set the value of
CATransform3DMakeScale to 0.5 for
both the x - and y -axes, and we've left the
z -axis alone. To create the shrink effect,
set the x and y values of the next
CATransform3DMakeScale to 1.0 ; by
setting the values of the x and y to 1.0 ,
we cause the zoom effect to reverse.
NOTE
The returned CATransform3D is wrapped in
an NSValue so that it can be used by
CABasicAnimation . Core Animation handles
pulling the CATransform3D back out of the
NSValue for us.
When CATransform3DMakeScale 's values are set, set the autoReverse flag to YES , give it a
nice slow duration of 1 second, and set a large repeat count; here we use 100 . Finally, we
add the newly created animation back to the layer using a key specific to this animation.
We want to use a large repeat count here so that we can give the illusion that the anima-
tion is going to continue forever. If we actually hit 100 iterations of the animation, it
would finally stop.
Using -rotateTransform:
The next transform that we apply rotates the layer. The layer rotates along one axis, and
then we auto-reverse the rotation, as shown in Listing 5-3.
LISTING 5-3
-rotateTransform:
- ( IBAction )rotateTransform:( id )sender;
{
NSValue *value = nil ;
 
 
Search WWH ::




Custom Search