如何为 UILabel 的 textColor 属性设置动画?
由于某种原因,当我尝试为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
相反,您是否尝试过像这样在对象本身上使用交叉淡入淡出过渡,它会给您带来从一种颜色到另一种颜色的漂亮淡入淡出效果:
Swift 5
Objective C 出于各种原因,这比使用 NSTimers、CATextLayers等
更好。 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
Objective C
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.
Swift 4解决方案:
Swift 4 solution:
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
这是唯一对我有用的东西:
This was the ONLY thing that worked for me :
您可以尝试创建 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).
这是我的代码,用于为标签文本颜色设置动画。 重要的部分是在动画之前将 textColor 设置为clearColor
Here's my code to animate label text color. The important part is to set textColor to clearColor before animation
这个答案已过时,并且不是原始问题的良好解决方案。 @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.
我发现将 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.
更新了 swift 3.0
我刚刚更改了 @budidino 评论
updated swift 3.0
I just changed from @budidino comments
感谢 @Strange 的回答,在 Swift 5、Xcode 11 中完美运行,但稍作语法更改。我正在为多个 UILabels 制作文本颜色动画,如果这也是您的情况,请尝试这个
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
尝试使用核心动画
Try using core animation