您能否告诉我如何动画UILabel的textColor
?如何动画UILabel的textColor?
例如,我想将颜色从白色更改为红色,然后回到带有淡入效果的白色并重复约3次。
我需要这个动画,因为我的应用程序从互联网上获取实时数据和当值改变我需要这个动画文本值告诉它改变用户。
非常感谢。
您能否告诉我如何动画UILabel的textColor
?如何动画UILabel的textColor?
例如,我想将颜色从白色更改为红色,然后回到带有淡入效果的白色并重复约3次。
我需要这个动画,因为我的应用程序从互联网上获取实时数据和当值改变我需要这个动画文本值告诉它改变用户。
非常感谢。
我不确定textColor是否直接在UILabel上动画。但是如果你使用CATextLayer它会更容易得到这种效果,
textColor不是动画的原因是UILabel使用常规的CALayer而不是CATextLayer。
要使textColor具有动画效果(以及文本,字体等),我们可以继承UILabel并使其使用CATextLayer。
这是一个相当大量的工作,但幸运的是我已经做到了:-)
你可以找到一个完整的解释+在这个article
+1:这非常有帮助。我尝试在GitHub上发送消息,但似乎没有设置电子邮件地址。自述文件中的“向您的项目添加AUIAnimatableText”部分需要更新以包含额外的步骤:“2.将UIFont + CoreTextExtensions.h + m添加到您的项目中”。再次感谢! – FreeAsInBeer 2012-02-10 14:05:37
有一个更简单的解决方案可用,请参阅[我的答案](http://stackoverflow.com/a/30506117/3451975)。 – Dschee 2015-05-28 12:14:05
问题的下拉开源替代品的UILabel很久以前就被问过了,但这里就是这样。
如上所述,UILabel
的textColor不具有可动画性。一个有用的技巧是在代码中动态创建另一个UILabel
,具有相同的属性和位置,但具有目标颜色。您可以将新的UILabel
的alpha从0.0增加到1.0,因此它看起来像原始UILabel的textColor是动画效果的。动画完成后,您可以移除其中一个标签。
下面是一个类级别方法的示例,该方法在短时间内更改为不同的textColor并将其更改回来。
+(void)colorizeLabelForAWhile:(UILabel *)label withUIColor:(UIColor *)tempColor animated:(BOOL)animated
{
// We will:
// 1) Duplicate the given label as a temporary UILabel with a new color.
// 2) Add the temp label to the super view with alpha 0.0
// 3) Animate the alpha to 1.0
// 4) Wait for awhile.
// 5) Animate back and remove the temporary label when we are done.
// Duplicate the label and add it to the superview
UILabel *tempLabel = [[UILabel alloc] init];
tempLabel.textColor = tempColor;
tempLabel.font = label.font;
tempLabel.alpha = 0;
tempLabel.textAlignment = label.textAlignment;
tempLabel.text = label.text;
[label.superview addSubview:tempLabel];
tempLabel.frame = label.frame;
// Reveal the temp label and hide the current label.
if (animated) [UIView beginAnimations:nil context:nil];
tempLabel.alpha = 1;
label.alpha = 0;
if (animated) [UIView commitAnimations];
// Wait for while and change it back.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, FOR_AWHILE_TIME*NSEC_PER_SEC), dispatch_get_main_queue(), ^{
if (animated) {
// Change it back animated
[UIView animateWithDuration:0.5 animations:^{
// Animate it back.
label.alpha = 1;
tempLabel.alpha = 0;
} completion:^(BOOL finished){
// Remove the tempLabel view when we are done.
[tempLabel removeFromSuperview];
}];
} else {
// Change it back at once and remove the tempLabel view.
label.alpha = 1.0;
[tempLabel removeFromSuperview];
}
});
}
是的,thans PostPCDev,你的回答是我所做的:) – 2012-10-08 07:18:36
这不是一个很好的解决方案,因为文本是antialiased。文字边缘附近会出现奇怪的伪影,尤其是在动画处理亮度不同的颜色时。 – 2016-06-21 01:27:34
这是一个古老的问题,但目前绝对有可能通过CrossDissolve动画文本颜色。以下是如何做到这一点在斯威夫特:
UIView.transitionWithView(self.newsLabel,
duration: 0.5,
options: UIViewAnimationOptions.TransitionCrossDissolve,
animations: {
self.newsLabel.textColor = self.inactiveForegroundColor
}, completion: nil)
谢谢,作品像魅力! :) – Dschee 2015-05-28 11:54:24
有使用方法从this answer两次是简单的解决方案没有任何的子类。
在Objective-C的一个可能的实现可能是这样的:
- (void)blinkTextInLabel:(UILabel *)label toColor:(UIColor *)color
{
[UIView transitionWithView:label duration:0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
// set to background color (change the color if necessary)
label.textColor = [UIColor whiteColor];
} completion:^(BOOL finished) {
[UIView transitionWithView:label duration:0.5f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
label.textColor = color;
} completion:nil];
}];
}
然后,只需调用辅助方法(这里使用UITableViewCell
同一类内):
[self blinkTextInLabel:myCell.textLabel toColor:[UIColor redColor]];
这个工作对我来说使用iOS 8。3 - 它可能也适用于其他版本,因为使用的方法自iOS 4.0起可用,但我没有测试过。
我希望这会有所帮助。
[UIView transitionWithView:myLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
label.textColor = [UIColor redColor];
} completion:^(BOOL finished) {
}];
试试:)
感谢this answer - 我实现了这一跨解散:
extension UILabel {
func animateTextColor(to color: UIColor) {
UIView.transition(with: self,
duration: 0.25,
options: .transitionCrossDissolve,
animations: { [weak self] in
self?.textColor = color
})
}
}
用法:
label.animateTextColor(to: .red)
能否请您解释一下这个,我Iphone开发中的新手:) – 2010-07-23 04:07:21
请在CoreAnimation和CALayers上阅读大量的文章:) – 2010-07-23 04:11:56
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html 它的主题太大了,无法进入这里。 – 2010-07-23 04:44:16