[英]How to animate the textColor property of an UILabel?
出於某種原因,當我嘗試為textColor
設置動畫時,它不起作用。 textColor
只是突然從 A 變為 B。是否可以對其進行動畫處理,例如,紅色變為黑色?
相反,您是否嘗試過像這樣在對象本身上使用交叉淡入淡出過渡,它會給您一個很好的從一種顏色到另一種顏色的淡入淡出效果:
目標 C
[UIView transitionWithView:myLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
myLabel.textColor = NEW_COLOR;
} completion:^(BOOL finished) {
}];
斯威夫特 5
UIView.transition(with: creditsLabel, duration: 0.25, options: .transitionCrossDissolve) {
self.creditsLabel.textColor = .red
}
由於各種原因,這比使用 NSTimers、CATextLayers 等要好。 CATextLayer 對文本字距調整或 NSAttributedText 沒有適當的支持,並且 NSTimers 滯后(加上代碼太多)。 上面的過渡動畫效果很好,也可以用在鏈式動畫中。 我遇到了同樣的問題,並且已經嘗試了上面的解決方案,但是這個簡單的代碼卻產生了奇跡。
斯威夫特 4解決方案:
UIView.transition(with: yourLabel, duration: 0.3, options: .transitionCrossDissolve, animations: {
self.yourLabel.textColor = .red
}, completion: nil)
textColor 不可設置動畫的原因是 UILabel 使用常規 CALayer 而不是 CATextLayer。
為了使 textColor 具有動畫效果(以及文本、字體等),我們可以將 UILabel 子類化以使其使用 CATextLayer。
這是相當多的工作,但幸運的是我已經做到了:-)
你可以找到一個完整的解釋+這一個簡易的開源替代品的UILabel 文章
您可以嘗試創建 UILabel 的另一個實例或任何具有 textColor 的實例,然后在這兩個實例之間應用動畫(舊的 textColor 和新的 textColor )。
這是唯一對我有用的東西:
let changeColor = CATransition()
changeColor.duration = 1
CATransaction.begin()
CATransaction.setCompletionBlock {
selected.label.layer.addAnimation(changeColor, forKey: nil)
selected.label.textColor = .blackColor()
}
selected.nameLabel.textColor = .redColor()
CATransaction.commit()
這是我為標簽文本顏色設置動畫的代碼。 重要的部分是在動畫之前將 textColor 設置為 clearColor
label.textColor = [UIColor clearColor];
[UIView transitionWithView:label duration:duration/4 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
label.textColor = self.highlightedCellPrimaryTextColor;
} completion:^(BOOL finished) { }];
此答案已過時,並且不是原始問題的好解決方案。 @strange 下面的答案要好得多,應該用它代替這個答案: https ://stackoverflow.com/a/20892927/76559
//下面的舊答案
textColor
屬性在文檔中沒有指定為可動畫,所以我認為你不能用一個簡單的 UIView 動畫塊來做到這一點......
這可能可以通過每隔幾毫秒觸發一次NSTimer
來完成,每次都將顏色逐漸從一個設置到另一個。
我說這很粗糙,因為它需要一個數組或其他一些包含從開始顏色到結束顏色的預設顏色值的容器,而且我確信有一種方法可以使用核心動畫或其他東西來做到這一點,我只是不不知道是什么
我發現在標簽下方放置一個 UIView 並為其不透明度設置動畫非常容易。 必須將標簽添加到該視圖中。 從資源消耗的角度來看,這可能不是一個好的解決方案,但非常簡單。
更新迅速 3.0
我剛從@budidino 評論改過來
UIView.transition(with: my.label, duration: 0.3, options: .transitionCrossDissolve, animations: { my.label.textColor = .black }, completion: nil)
感謝 @ 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)
}
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()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.