[英]How to animate text color of a UILabel like a progress bar
我試圖從左到右逐漸更改UILabel的文本顏色。 我發現我可以使用UIView的動畫:
[UIView transitionWithView:_label duration:1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
_label.textColor = [UIColor redColor];
} completion:nil];
但是問題是我的情況沒有選擇:從左到右逐漸遞增。
然后,我找到了一種通過調用逐個字母地更改文本顏色的方法:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(yourSelector) userInfo:nil repeats:NO];
- (void)yourSelector
{
[_string setAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} range:NSMakeRange(_index, 1)];
[_label setAttributedText:_string];
_index++;
}
但是這里的問題是,我希望動畫更像一個字母一個字母,但每個字母也具有動畫效果,例如在同一位置放置兩個具有不同顏色的標簽(黑色覆蓋紅色),並從左到右設置蒙版動畫可以隱藏黑色標簽,以便逐漸顯示紅色標簽。
那么有沒有辦法做到這一點?
為此,請按照以下步驟操作:
UILabel
( trackLabel
和progressLabel
)。 trackLabel
具有blackColor
, progressLabel
具有redColor
。 progressLabel
與trackLabel
重疊,相同的前導,頂部和底部。 trackLabel
全寬度的文本, progressLabel
0寬度。 progressLabel.lineBreakMode = NSLineBreakByClipping;
progressLabel
寬度。 有關更多詳細信息,您可以檢查我的演示項目 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.