Vous pouvez l'animer, mais j'ai dû d'abord le régler (par programme) sur clearColor pour une raison quelconque. Sans cela, l'animation ne fonctionnait pas ou n'était pas visible.
Je suis en train d'animer la couleur de fond d'un UILabel dans une cellule de tableau personnalisée. Voici le code de la méthode willDisplayCell. Je n'essaierais pas de définir l'animation dans cellForRow, car une grande partie de la mise en page est modifiée par le tableau.
- (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];
}
Voici ma routine d'animation :
-(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"];
}
2 votes
Pour les futurs lecteurs, ceci n'est pas spécifique à UILabel, mais l'exigence est qu'une vue doit commencer avec une couleur de fond, même si cette couleur est claire. Il n'est pas nécessaire d'animer sur le calque, le UIView.backgroundColor fonctionne très bien, à condition de respecter ce qui précède.