Graphics Reference
In-Depth Information
LISTING 6-6
Add Blur Animation to the mouseDown Event
- ( void )mouseDown:(NSEvent *)theEvent;
{
CABasicAnimation *anim =
[ CABasicAnimation animationWithKeyPath :
@”filters.blur.inputRadius” ];
// Set the filter's start value
[anim setFromValue :[ NSNumber numberWithFloat :0.0f]];
// Set the filter's ending value
[anim setToValue:[ NSNumber numberWithFloat :5.0f]];
[anim setDuration :0.2f];
[anim setAutoreverses : YES ];
[anim setRemovedOnCompletion : YES ];
[blurLayer addAnimation :anim forKey : @”filters.blur.inputRadius” ];
}
As shown in Listing 6-6, we create a basic animation using the key-path filters.blur.
inputRadius . You might have already noticed, but this is where the name of the filter we
added in Listing 6-2 becomes important. The filters field is being accessed here, and we
are querying for a filter named blur (the name of the Gaussian blur filter we added in
Listing 6-2), so we can animate its inputRadius .
The starting and ending values for the blur filter's inputRadius are set using the
- setFromValue: and - setToValue: methods, as shown in the next two lines. We are
going to animate the input radius from 0.0 to 5.0. Next, we set the duration of the
animation to run quickly; approximately one-fifth of a second, as noted with
- setDuration:0.2f .
The - setAutoreverses:YES call tells the animation to loop backward to its original value.
If we left this to its default ( NO ), when the animation finished we would see the layer
instantly snap back to its original value, which makes it look jerky. In this situation, it is
ideal to have it animate back to the original value to give the animation a much
smoother, polished look.
Finally, the animation is automatically set to be removed (- setRemovedOnCompletion:YES )
from the animation when it has finished one iteration. This is actually the default behav-
ior so this is a superfluous call. However, we include it here so you understand that this is
the desired action.
Now, when the button is clicked, it blurs the entire button layer to a 5.0 pixel radius and
returns to its clear state within one-fifth of a second.
 
Search WWH ::




Custom Search