Animating foregroundColor property of CATextLayer
I want to animate the change in the color of text(foregroundColor) of a CATextLayer on the iPhone. According to the documentation, implicit animation of the property is not available on OSX 10.6, but no mention is made of iOS. As a result i've just assumed that the animation must be explicit for it to work on iOS.
I've changed the backing layer class using +(class)layerClass
, and set an initial value for the property in the UIView init
method. The text shows up just fine, along with all of the properties i've applied (shadow, font etc), yet the animation fails to have any impact.
Below is my animation method, that i call from a KVO observation method so that the view alerts the user when the observed property has changed.
-(void) animateTextChange{
animation = [CABasicAnimation animationWithKeyPath:@"foregroundColor"];
[animation setFromValue:[[UIColor blackColor] CGColor]];
[animation setToValue:[[UIColor whiteColor] CGColor]];
[animation setDuration:2.0f];
[[self layer] addAnimation:animation
forKey:@"foregroundColor"
];
}
开发者_如何学C
Thanks as always.
This is the only way i succeeded to do that:
[CATransaction begin];
[CATransaction setAnimationDuration:.5];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
yellowWord.foregroundColor =[UIColor redColor].CGColor;
[CATransaction commit];
Hope it will help someone
精彩评论