Graphics Programs Reference
In-Depth Information
Implicitly Animatable Properties
Several of the properties of
CALayer
are
implicitly animatable
. This means that changes
to these properties are automatically animated when their setters are called. The property
position
is an implicitly animatable property. Sending the message
setPosition:
to
a
CALayer
doesn't just move the layer to a new position; it animates the change from the
old position to the new one.
In this section, you will have the application respond to user taps: the
boxLayer
will
move to wherever the user starts a touch. This change in position will be animated because
position
is an implicitly animatable property.
In
HypnosisView.m
, implement
touchesBegan:withEvent:
to change the lay-
er's position.
- (void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self];
[boxLayer setPosition:p];
}
Build and run the application. The layer will move smoothly from its current position to
where you tap. All you had to do to get this animation was send
setPosition:
. Pretty
cool, huh?
If the user drags rather than taps, let's have the layer follow the user's finger. In
Hypnos-
isView.m
, implement
touchesMoved:withEvent:
to send
setPosition:
to the
layer.
- (void)touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self];
[boxLayer setPosition:p];
}
Build and run the application. This is not so cool. Notice how the animation makes the lay-
er lag behind your dragging finger, making the application seem sluggish.
Why the poor response? An implicitly animatable property changes to its destination value
over a constant time interval. However, changing a property while it is being animated