Graphics Reference
In-Depth Information
Making the Effect “Sticky”
There might be times when you want the animation effect to be sticky, where rather than
resetting the animated property back to its original value, you want its new value to
“stick.” To see an example of this, go to the companion website and launch the Click to
Blur Sticky project in Xcode and click Build and Go. When you click on one of the icons,
you notice that the icon stays blurred until you click it a second time, as shown in
Figure 6-3.
To create this effect, so the clicked icon stays blurred and then returns to its normal state
when clicked a second time, you need to keep track of the button state. This is accom-
plished using a BOOL value, which for this example is named toggle . The - mouseDown code
now looks like what we see in Listing 6-7.
LISTING 6-7
Implementing Toggle Functionality
- ( void )mouseDown:(NSEvent *)theEvent;
{
CABasicAnimation *anim =
[ CABasicAnimation
animationWithKeyPath : @”filters.blur.inputRadius” ];
if (toggle)
{
[anim setFromValue :[ NSNumber numberWithFloat :0.0f]];
[anim setToValue:[ NSNumber numberWithFloat :5.0f]];
}
else
{
[anim setFromValue :[ NSNumber numberWithFloat :5.0f]];
[anim setToValue:[ NSNumber numberWithFloat :0.0f]];
}
[anim setDuration :0.2f];
[anim setRemovedOnCompletion : NO ];
[anim setFillMode : kCAFillModeForwards ];
[blurLayer addAnimation :anim
forKey : @”filters.blur.inputRadius” ];
toggle = !toggle;
}
So what changed? The first thing you notice is that depending on the current state of
the toggle variable, we determine whether the start value is 0.0 or 5.0 before setting
- setToValue: to the opposite value. If our toggle is not yet set, we want to animate to
 
Search WWH ::




Custom Search