3 回答

TA貢獻2065條經驗 獲得超14個贊
我在任何地方都找不到文檔,但是它的backgroundColor屬性似乎UILabel不是可動畫的,因為您的代碼可以在vanilla上正常工作UIView。但是,只要您不設置標簽視圖本身的背景顏色,此hack似乎就可以起作用:
#import <QuartzCore/QuartzCore.h>
...
theLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;
[UIView animateWithDuration:2.0 animations:^{
theLabel.layer.backgroundColor = [UIColor greenColor].CGColor;
} completion:NULL];

TA貢獻1831條經驗 獲得超4個贊
您可以為其設置動畫,但是由于某種原因,我必須首先(以編程方式)將其設置為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"];
}
- 3 回答
- 0 關注
- 551 瀏覽
添加回答
舉報