Leider ist die Farbe animieren nicht mit UIView Animationen. Die Antworten auf this question geben einige gute Umgehungslösungen, ohne Core Animation zu verwenden.
Wenn Sie dies nicht mit einem CATextLayer anstelle einer UILabel ausmacht, dann kann die Farbe (und die Skalierung in Ihrem Beispiel) animiert werden:
#import <QuartzCore/QuartzCore.h>
//Also need to add QuartzCore framework to project (as well as the import).
//
-(void)animateTextLayer {
CGFloat animationDuration = 5;
CATextLayer *textLayer = [CATextLayer layer];
[textLayer setString:@"Hello World"];
[textLayer setForegroundColor:[UIColor purpleColor].CGColor];
[textLayer setFontSize:30];
[textLayer setFrame:CGRectMake(20, 200, 300, 40)];
[[self.view layer] addSublayer:textLayer];
CABasicAnimation *colorAnimation = [CABasicAnimation
animationWithKeyPath:@"foregroundColor"];
colorAnimation.duration = animationDuration;
colorAnimation.fillMode = kCAFillModeForwards;
colorAnimation.removedOnCompletion = NO;
colorAnimation.fromValue = (id)[UIColor purpleColor].CGColor;
colorAnimation.toValue = (id)[UIColor greenColor].CGColor;
colorAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionLinear];
CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation
animationWithKeyPath:@"transform"];
NSArray *scaleValues = [NSArray arrayWithObjects:
[NSValue valueWithCATransform3D:CATransform3DScale(textLayer.transform, 1, 1, 1)],
[NSValue valueWithCATransform3D:CATransform3DScale(textLayer.transform, 1.5, 1.5, 1)],
[NSValue valueWithCATransform3D:CATransform3DScale(textLayer.transform, 0.5, 0.5, 1)], nil];
[scaleAnimation setValues:scaleValues];
scaleAnimation.fillMode = kCAFillModeForwards;
scaleAnimation.removedOnCompletion = NO;
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = animationDuration;
animationGroup.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionLinear];
animationGroup.fillMode = kCAFillModeForwards;
animationGroup.removedOnCompletion = NO;
animationGroup.animations =
[NSArray arrayWithObjects:colorAnimation, scaleAnimation, nil];
[textLayer addAnimation:animationGroup forKey:@"animateColorAndScale"];
}
wenn nur gäbe es eine Möglichkeit geben, – bioffe
eine vertikale Ausrichtung in CATextLayer zu setzen hier ein _much_ bessere Abhilfe nur mit dem Original UILabel selbst: http: // Stackoverflow .com/questions/6442774/is-uilabels-backgroundcolor-nicht-animierbar/6442868 # 6442868 – Anna
@AnnaKarenina Der Link, auf den Sie hingewiesen haben, ist die Hintergrundfarbe zu animieren. – xhan