简体   繁体   English

设置UILabel动画以更改颜色?

[英]Animate UILabel to Change Colour?

I would like to make the text in my UILabel called coloursLabel change text colour by itself. 我想让我的UILabel的文本称为coloursLabel更改文本颜色。 I have tried the following method as another SO answer suggested (albeit for the backgroundColor ) but it still doesn't change. 我尝试了以下方法作为另一个SO答案的建议(尽管是backgroundColor ),但它仍然没有改变。 Am I missing something silly? 我想念一些愚蠢的东西吗?

UILabel *coloursLabel = [[UILabel alloc] init];
    coloursLabel.text = @"This sentence is colourful!";
    [coloursLabel sizeToFit];
    coloursLabel.center = self.view.center;
    coloursLabel.frame = CGRectOffset(coloursLabel.frame, timeForPage(6), 0);

[UIView animateWithDuration:2.0 animations:^{
    coloursLabel.textColor = [UIColor greenColor];
    coloursLabel.textColor = [UIColor redColor];
    coloursLabel.textColor = [UIColor blueColor];
    coloursLabel.textColor = [UIColor yellowColor];
    coloursLabel.textColor = [UIColor blackColor];
} completion:NULL];

coloursLabel shows the correct text but the text is in black. coloursLabel显示正确的文本,但文本为黑色。

Any help would be much appreciated! 任何帮助将非常感激!

UILabel's textColor is not animatable. UILabel的textColor不能设置动画。 You could write your own Label class, using a CATextLayer as mask and animate its layer's backgroundColor. 您可以使用CATextLayer作为遮罩并为其图层的backgroundColor设置动画,从而编写自己的Label类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM