Graphics Reference
In-Depth Information
layer's property so that when the animation has finished, the property you animated will
actually be set in the layer to the to-value you specified.
You can simply cause your animation to stop at the end point you specify, but this is only
a visible stickiness, if you will. The internal value is still the same. To actually change the
internal model value, you have to explicitly set the property in question. For example, to
explicitly set the position property, you need to call -setPosition on the layer. This
creates a little problem, though.
If you set the value of a property by calling - set on that property explicitly, the default
animation will be used rather than one you might set for the property you are animating.
Listing 3-9 demonstrates one way you might try to set the position. Notice that we have
created a basic animation to use for the position property; however, the explicit call to
-setPosition on the layer overrides the animation we set in the line that follows it,
making the basic animation completely useless. If you try this code, you see that
although our layer ends up in the right position, it uses the default duration of 0.25
seconds rather than the 5 seconds we have explicitly set in the animation.
LISTING 3-9
Animating and Updating the Position Property
CABasicAnimation *animation =
[ CABasicAnimation animationWithKeyPath : @”position” ];
[animation setFromValue :[ NSValue valueWithPoint:startPoint]];
[animation setToValue:[ NSValue valueWithPoint:endPoint]];
[animation setDuration :5.0];
[layer setPosition :endpoint];
[layer addAnimation :animation forKey : nil ];
So now the question becomes, how can you get the animation to use the specified dura-
tion? Take a look at the last line in Listing 3-9. Notice that the forKey: parameter of the
call is set to nil . This is the reason why the animation is not overriding the default. If
you change the last line to [layer addAnimation:animation forKey:@”position”] , the
animation will work using the duration as expected. This tells the layer to use the new
animation we have specified for this keypath whenever it needs to be animated.
Implicit Layer Animation and the Default Timing Function
We can use the CATransaction class to override the default duration as we previously did
in this chapter, and it does make it simple to animate the layer using the duration we
specify. If we use the code in Listing 3-10, the position property is set in the layer and
the property is animated on its way there as you might expect.
 
Search WWH ::




Custom Search