這看起來應該可以工作,但不會。顏色一次變成綠色。如何動畫UILabel的背景顏色?
self.labelCorrection.backgroundColor = [UIColor whiteColor];
[UIView animateWithDuration:2.0 animations:^{
self.labelCorrection.backgroundColor = [UIColor greenColor];
}];
這看起來應該可以工作,但不會。顏色一次變成綠色。如何動畫UILabel的背景顏色?
self.labelCorrection.backgroundColor = [UIColor whiteColor];
[UIView animateWithDuration:2.0 animations:^{
self.labelCorrection.backgroundColor = [UIColor greenColor];
}];
我找不到它記錄在任何地方,但它似乎UILabel
的backgroundColor
屬性不是動畫,因爲您的代碼工作正常與香草UIView
。該黑客似乎但是上班,只要你不設置標籤視圖本身的背景色:
#import <QuartzCore/QuartzCore.h>
...
theLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;
[UIView animateWithDuration:2.0 animations:^{
theLabel.layer.backgroundColor = [UIColor greenColor].CGColor;
} completion:NULL];
這個工程,謝謝!我仍然想知道爲什麼一個UIView可以工作,而UILabel不會,畢竟UILabel是一個UIView。 – 2010-09-21 19:05:09
同意。我只能假設,在這一點上,蘋果選擇使這些屬性爲'UILabel'非動畫。 FWIW,「UIButton」的行爲與標籤相同。 – bosmacs 2010-09-21 19:13:31
奇怪的是,如果'UILabel'來自xib,至少'frame'屬性似乎也不是動畫。如果它是以編程方式創建的,它確實有效,儘管......很奇怪。 – 2011-08-08 06:02:09
這裏是參考:
動畫含有 的變化提交到視圖A塊對象。 這是編程式 更改視圖層次結構中視圖的任何動畫屬性的位置。此 塊不採用參數,也沒有 返回值。此參數不能 爲NULL
什麼我瞭解,這是當你的看法致電動畫塊,那麼你的視圖標籤背景顏色將提交給是自那時以來,綠色環保。然後,如果您不將標籤背景顏色更改爲其他顏色,那麼它將始終呈綠色。這是我猜
手動執行彩動畫,基於關閉一個NSTimer或CADisplayLink回調的一些合理的框架費率(比如20fps)。您將不得不根據整個動畫時間(在您的示例中爲2.0秒)中的部分流逝時間計算RGB或HSV中自己的顏色變化曲線,或使用40箇中間色的數組。
它說在backgroundColor時動畫
真的不知道從什麼時候起的文檔。
如果你不想陷入Quartz中,根據回答之前的答案,創建一個與UILabel具有相同大小的空UIView並將它放置在與UILabel相同的位置(並且按照Z順序,在它下面),你可以爲UIView的背景顏色設置動畫效果(我已經嘗試過了,它適用於我)。
您可以爲它設置動畫效果,但是我必須首先將其設置爲(以編程方式)clearColor出於某種原因。沒有這個,動畫要麼不工作要麼不可見。
我在UILabel的自定義表格單元格的背景顏色動畫。這是willDisplayCell方法中的代碼。我不會嘗試在cellForRow中設置動畫,因爲很多佈局都被表格修飾了。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
((RouteListCell *)cell).name.backgroundColor = [UIColor clearColor];
[((RouteListCell *)cell).name backgroundGlowAnimationFromColor:[UIColor whiteColor] toColor:[UIColor redColor] clearAnimationsFirst:YES];
}
這裏是我的動畫程序:
-(void) backgroundGlowAnimationFromColor:(UIColor *)startColor toColor:(UIColor *)destColor clearAnimationsFirst:(BOOL)reset;
{
if (reset)
{
[self.layer removeAllAnimations];
}
CABasicAnimation *anAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
anAnimation.duration = 1.00;
anAnimation.repeatCount = HUGE_VAL;
anAnimation.autoreverses = YES;
anAnimation.fromValue = (id) startColor.CGColor; // [NSNumber numberWithFloat:1.0];
anAnimation.toValue = (id) destColor.CGColor; //[NSNumber numberWithFloat:0.10];
[self.layer addAnimation:anAnimation forKey:@"backgroundColor"];
}
請試試這個,
[UIView transitionWithView:yourView duration:0.2 options:UIViewAnimationOptionTransitionNone animations:^{
[yourView setBackgroundColor:[UIColor colorWithRed:0.8588 green:0.8588 blue:0.8588 alpha:1]];
}completion:^(BOOL finished) {
}];
這對我的作品。
我最近再次遇到這個問題,經過一番研究,我發現基本上沒有任何變化(可能不會在可預見的未來),所以我最終使用Facebook POP framework(不僅僅是)。
您可以輕鬆地做視圖和層基本屬性的動畫,但除此之外,它提供了顏色之間的平滑過渡(基本上任何你想要的,甚至是你的自定義類型,有一些額外的編碼)。因此,對於背景顏色,你會做這樣的事情:
// Create spring animation (there are more types, if you want)
let animation = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)
// Configure it properly
animation.autoreverses = false
animation.removedOnCompletion = true
animation.fromValue = UIColor.yourBeginningColor()
animation.toValue = UIColor.yourFinalColor()
// Add new animation to your view - animation key can be whatever you like, serves as reference
label.pop_addAnimation(animation, forKey: "YourAnimationKey")
中提琴,現在你可以在具有該屬性的一切動畫的背景顏色!
(重要)設置UILabel的背景色以清除(無論是在IB或代碼)。例如:
override func viewDidLoad() {
super.viewDidLoad()
myLabel.backgroundColor = UIColor.clear
}
爲圖層背景色設置動畫效果。
@IBAction func animateButtonTapped(sender: UIButton) {
UIView.animate(withDuration: 1.0, animations: {
self.myLabel.layer.backgroundColor = UIColor.red.cgColor
})
}
注意CGColor
是UIColor
後加入。
結果
斯威夫特3
要動畫從白色到綠色,設置您的標籤,這樣你的標籤背景色:
self.labelCorrection.backgroundColor = UIColor.clear
self.labelCorrection.layer.backgroundColor = UIColor.white.cgColor
動畫像這樣:
UIView.animate(withDuration: 2.0) {
self.labelCorrection.layer.backgroundColor = UIColor.green.cgColor
}
要返回到原來的狀態,這樣你就可以再次動畫,請確保您刪除的動畫:
self.labelCorrection.layer.backgroundColor = UIColor.white.cgColor
self.labelCorrection.layer.removeAllAnimations()
對於任何未來的讀者,這不是針對的UILabel,但要求是視圖必須以背景顏色開始,即使該顏色清晰。你不需要在圖層上設置動畫,UIView.backgroundColor可以正常工作,如上所述。 – 2018-02-01 20:36:57