iPad:动画UILabels变色

Oh *_*Boy 19 iphone animation ios

我有一个标签,在我的应用程序中弹出显示点.我使用以下代码使标签的规模变大,然后变小.我还想将颜色从紫色变为绿色.有人能指出我实现这一目标的资源吗?

mLabel.textColor = [UIColor purpleColor];

 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration:1.0];
 [UIView setAnimationDelegate:self];
 mLabel.transform = CGAffineTransformMakeScale(1.5,1.5);
 [UIView setAnimationRepeatCount:1];
 mLabel.transform = CGAffineTransformMakeScale(0.5,0.5);
 mLabel.textColor = [UIColor greenColor];
 [UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

CIF*_*ter 67

您可以使用iOS 4.0的视图转换方法来执行此操作:

[UIView transitionWithView:statusLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    [statusLabel setTextColor:[UIColor whiteColor]];
    [statusLabel setShadowColor:[UIColor blackColor]];
} completion:nil];
Run Code Online (Sandbox Code Playgroud)


小智 27

不幸的是,UIView动画的颜色没有动画效果.这个问题的答案在不使用Core Animation的情况下提供了一些很好的解决方法.

如果你不介意使用CATextLayer而不是UILabel,那么颜色(以及你的例子中的缩放)可以像这样动画:

#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"];
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个使用原始UILabel本身的更好的解决方法:http://stackoverflow.com/questions/6442774/is-uilabels-backgroundcolor-not-animatable/6442868#6442868 (4认同)
  • @AnnaKarenina你指出的链接是动画背景颜色. (2认同)

ada*_*ton 6

textColor不可动画的原因是UILabel使用常规CALayer而不是CATextLayer.

为了使textColor可以生成动画(以及文本,字体等),我们可以继承UILabel并使其使用CATextLayer.

这是相当多的工作,但幸运的是我已经做到了:-)

你可以找到一个完整的解释+这一个简易的开源替代品的UILabel 文章