如何为 UILabel 的 textColor 属性设置动画?

发布于 2024-08-25 07:17:51 字数 113 浏览 8 评论 0原文

由于某种原因,当我尝试为 textColor 设置动画时,它不起作用。 textColor 突然从 A 变为 B。是否可以对其进行动画处理,例如从红色变为黑色?

For some reason, when I try to animate textColor, it won't work. The textColor just suddenly changes from A to B. Is it possible to animate it, for example, red to black?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

等风也等你 2024-09-01 07:17:51

相反,您是否尝试过像这样在对象本身上使用交叉淡入淡出过渡,它会给您带来从一种颜色到另一种颜色的漂亮淡入淡出效果:

Swift 5

UIView.transition(with: creditsLabel, duration: 0.25, options: .transitionCrossDissolve) {
    self.creditsLabel.textColor = .red
}

Objective C 出于各种原因,这比使用 NSTimers、CATextLayers等

[UIView transitionWithView:myLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    myLabel.textColor = NEW_COLOR;
} completion:^(BOOL finished) {
}];

更好。 CATextLayer 没有对文本字距调整或 NSAttributedText 的适当支持,并且 NSTimers 很滞后(而且代码太多)。上面的过渡动画就达到了目的,也可以在链式动画中使用。我遇到了同样的问题,并且已经尝试了上面的解决方案,但这个简单的代码却产生了奇迹。

Instead, have you tried using a crossfade transition on the object itself like this, it'll give you a nice fade-in fade-out effect from one color to another:

Swift 5

UIView.transition(with: creditsLabel, duration: 0.25, options: .transitionCrossDissolve) {
    self.creditsLabel.textColor = .red
}

Objective C

[UIView transitionWithView:myLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    myLabel.textColor = NEW_COLOR;
} completion:^(BOOL finished) {
}];

This is better than using NSTimers, CATextLayers and so on so forth for various reasons. CATextLayer does not have proper support for text kerning or NSAttributedText, and NSTimers are laggy (plus there's too much code). The transition animation above does the trick, and also can be used in a chain animation. I had the same issue and have already tried the solutions above but this simple code works wonders instead.

灰色世界里的红玫瑰 2024-09-01 07:17:51

Swift 4解决方案:

UIView.transition(with: yourLabel, duration: 0.3, options: .transitionCrossDissolve, animations: {
  self.yourLabel.textColor = .red
}, completion: nil)

Swift 4 solution:

UIView.transition(with: yourLabel, duration: 0.3, options: .transitionCrossDissolve, animations: {
  self.yourLabel.textColor = .red
}, completion: nil)
倥絔 2024-09-01 07:17:51

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

为了使 textColor 可动画化(以及文本、字体等),我们可以对 UILabel 进行子类化,使其使用 CATextLayer。

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

您可以在此 文章

The reason that textColor is not animatable is that UILabel uses a regular CALayer instead of a CATextLayer.

To make textColor animatable (as well as text, font, etc.) we can subclass UILabel to make it use a CATextLayer.

This is quite a lot of work, but luckily I already did it :-)

You can find a complete explanation + a drop-in open source replacement for UILabel in this article

︶ ̄淡然 2024-09-01 07:17:51

这是唯一对我有用的东西:

let changeColor = CATransition()
changeColor.duration = 1

CATransaction.begin()

CATransaction.setCompletionBlock {
    selected.label.layer.add(changeColor, forKey: nil)
    selected.label.textColor = .black
}

selected.nameLabel.textColor = .red

CATransaction.commit()

This was the ONLY thing that worked for me :

let changeColor = CATransition()
changeColor.duration = 1

CATransaction.begin()

CATransaction.setCompletionBlock {
    selected.label.layer.add(changeColor, forKey: nil)
    selected.label.textColor = .black
}

selected.nameLabel.textColor = .red

CATransaction.commit()
智商已欠费 2024-09-01 07:17:51

您可以尝试创建 UILabel 的另一个实例或具有 textColor 的任何实例,然后在这两个实例(具有旧 textColor 的实例和具有新 textColor 的实例)之间应用动画。

You could try creating another instance of the UILabel or whatever it is that has the textColor, and then apply the animation between those two instances (the with the old textColor and the one with the new textColor).

居里长安 2024-09-01 07:17:51

这是我的代码,用于为标签文本颜色设置动画。 重要的部分是在动画之前将 textColor 设置为clearColor

label.textColor = [UIColor clearColor];
[UIView transitionWithView:label duration:duration/4 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    label.textColor = self.highlightedCellPrimaryTextColor;
} completion:^(BOOL finished) { }];

Here's my code to animate label text color. The important part is to set textColor to clearColor before animation

label.textColor = [UIColor clearColor];
[UIView transitionWithView:label duration:duration/4 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    label.textColor = self.highlightedCellPrimaryTextColor;
} completion:^(BOOL finished) { }];
无戏配角 2024-09-01 07:17:51

这个答案已过时,并且不是原始问题的良好解决方案。 @strange 下面的答案更好,应该用来代替这个答案: https://stackoverflow.com/a/20892927/ 76559

//下面的旧答案

textColor 属性在文档中没有指定为可动画,所以我认为你不能用简单的 UIView 动画块来做到这一点...

这可以通过每几毫秒触发一次 NSTimer 来非常粗略地完成,每次将颜色逐渐从一种设置为另一种。

我说这很粗糙,因为它需要一个数组或其他一些包含从开始颜色到完成颜色的预设颜色值的容器,并且我确信有一种方法可以使用核心动画或其他东西来做到这一点,我只是不知道不知道那是什么。

This answer is obsolete, and is not a good solution for the original question. @strange 's answer below is much better and should be used instead of this answer: https://stackoverflow.com/a/20892927/76559

//Old answer below

The textColor property is not specified as being animatable in the docs, so I don't think you can do it with a simple UIView animations block...

This could probably be done pretty crudely with an NSTimer firing every couple of milliseconds, each time setting the colour gradually from one to the other.

I say this is crude because it would require an array or some other container of preset colour values going from the start colour to the finish colour, and I'm sure there's a way you could do this using core animation or something, I just don't know what it is.

紫竹語嫣☆ 2024-09-01 07:17:51

我发现将 UIView 放置在标签下方并为其不透明度设置动画非常容易。
必须将标签添加到该视图中。
从资源消耗的角度来看,这可能不是一个好的解决方案,但非常简单。

I found it pretty easy placing an UIView below the label and animating its opacity.
The label has to be added to that view.
Maybe not a good solution from the resources consumption point of view, but pretty straightforward.

本宫微胖 2024-09-01 07:17:51

更新了 swift 3.0

我刚刚更改了 @budidino 评论

UIView.transition(with: my.label, duration: 0.3, options: .transitionCrossDissolve, animations: { my.label.textColor = .black }, completion: nil)

updated swift 3.0

I just changed from @budidino comments

UIView.transition(with: my.label, duration: 0.3, options: .transitionCrossDissolve, animations: { my.label.textColor = .black }, completion: nil)
知你几分 2024-09-01 07:17:51

感谢 @Strange 的回答,在 Swift 5、Xcode 11 中完美运行,但稍作语法更改。我正在为多个 UILabels 制作文本颜色动画,如果这也是您的情况,请尝试这个

for label in [label1, label2, ...] as [UILabel] { // loop through a @IBOutlet UILabel array
        UIView.transition(with: label, duration: 0.3, options: .transitionCrossDissolve, animations: {
            label.textColor = .label
        }, completion: nil)
}

Thanks for @Strange's answer, working perfectly in Swift 5, Xcode 11 with a bit syntax change. I was animating text color for multiple UILabels, if this is also your case, try this

for label in [label1, label2, ...] as [UILabel] { // loop through a @IBOutlet UILabel array
        UIView.transition(with: label, duration: 0.3, options: .transitionCrossDissolve, animations: {
            label.textColor = .label
        }, completion: nil)
}
何以心动 2024-09-01 07:17:51

尝试使用核心动画

   func textColorBlinking(){
        let changeColor = CATransition()
        changeColor.duration = 1
        changeColor.type = .fade
        changeColor.repeatCount = Float.infinity
        CATransaction.begin()
        CATransaction.setCompletionBlock {
            self.lblHome.layer.add(changeColor, forKey: nil)
            self.lblHome.textColor = .white
        }
        self.lblHome.textColor = .red
        CATransaction.commit()
   }

Try using core animation

   func textColorBlinking(){
        let changeColor = CATransition()
        changeColor.duration = 1
        changeColor.type = .fade
        changeColor.repeatCount = Float.infinity
        CATransaction.begin()
        CATransaction.setCompletionBlock {
            self.lblHome.layer.add(changeColor, forKey: nil)
            self.lblHome.textColor = .white
        }
        self.lblHome.textColor = .red
        CATransaction.commit()
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文