Graphics Reference
In-Depth Information
CAAnimationDelegate is an ad hoc protocol, so you won't find a CAAnimationDelegate
@protocol defined in any header file, but you can find the supported methods in the
CAAnimation header or in Apple's developer documentation. In this case, we use the
-animationDidStop:finished: method to update our layer's backgroundColor
immediately after the animation has finished.
We need to set up a new transaction and disable layer actions when we update the property;
otherwise, the animation will occur twice—once due to our explicit CABasicAnimation ,
and then again afterward due to the implicit animation action for that property. See Listing
8.3 for the complete implementation.
Listing 8.3 Fixing the Background Color Value Once Animation Completes
@implementation ViewController
- ( void )viewDidLoad
{
[ super viewDidLoad ];
//create sublayer
self .colorLayer = [ CALayer layer ];
self .colorLayer. frame = CGRectMake ( 50.0f , 50.0f , 100.0f , 100.0f );
self .colorLayer. backgroundColor = [ UIColor blueColor ]. CGColor ;
//add it to our view
[ self .layerView. layer addSublayer : self .colorLayer];
}
- ( IBAction )changeColor
{
//create a new random color
CGFloat red = arc4random () / ( CGFloat ) INT_MAX ;
CGFloat green = arc4random () / ( CGFloat ) INT_MAX ;
CGFloat blue = arc4random () / ( CGFloat ) INT_MAX ;
UIColor *color = [ UIColor colorWithRed :red
green :green
blue :blue
alpha : 1.0 ];
//create a basic animation
CABasicAnimation *animation = [ CABasicAnimation animation ];
animation. keyPath = @"backgroundColor" ;
animation. toValue = ( __bridge id )color. CGColor ;
animation. delegate = self ;
//apply animation to layer
Search WWH ::




Custom Search