Is UILabel's backgroundColor not animatable?
Asked Answered
H

1

3

I have a gridView, I use to display several GridViewCell : UIView. The GidViewCell adds an UILabel to itself and attaches anUITapGestureRecognizer to itself.

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[self addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];

in tapped: I want to play some animation, including changing the background color of the label

- (void) tapped:(id)sender {
    [UIView animateWithDuration:1.0
                          delay:0
                        options:(UIViewAnimationOptionAllowUserInteraction)
                     animations:^{ 
                         [(UILabel *)[[self subviews] objectAtIndex:0] setBackgroundColor:[UIColor blueColor]];
                     }  
                     completion:^(BOOL finished){
                         [(UILabel *)[[self subviews] objectAtIndex:0] setBackgroundColor:[UIColor whiteColor]];
                     }
     ];     
[self.delegate didSelectGridViewCell:self];
}

But the label just flashes blue for a blink of an eye — if at all. If I use the same code but adjust the alpha of the label instead of the background color, everything works as expected.

Apple's docs lists backgroundColor as animatable. Is it? Am I doing something wrong?

Headsail answered 22/6, 2011 at 15:49 Comment(5)
@bioffe as this is not a iPhone specific question, I'd say, [ios] would be the better tagHeadsail
Yes, [ios] is better tag. Thank you. BTW my UILabel's background does not animate with this solution. I have much better luck with CATextLayer adding to UILabel.layer.Housework
Try this, no need to do all that: https://mcmap.net/q/224631/-how-to-animate-the-textcolor-property-of-an-uilabelProprietary
@strange, you don't need to raise noise level on a 3 years old question with accepted answer.Headsail
@Headsail The SDK has changed in 3 years and this is quite a popular question. I landed on the page while looking for a solution and found one so wanted to share. My recommended approach is cleaner and easier and does not require above mentioned hacks (no disrespect to your answer). Apologies if that offended you.Proprietary
F
11

I've never tried this myself but I know of someone who has. I don't know if this is a bug in the documentation or in the code. I'm assuming the latter since UILabel inherits from UIView and I know for sure that you're able to animate the background of a UIView. The workaround apparently is to drill down to the underlying CALayer and set the background color there.

Flop answered 22/6, 2011 at 15:55 Comment(3)
works! [[(UILabel *)[[self subviews] objectAtIndex:0] layer] setBackgroundColor:[UIColor blueColor].CGColor];Headsail
@vikingosegundo: just for readability, there's no need to cast the first subview to UILabel. All UIViews have a layer with a backgroundColor you can animate. Even better, store a reference to the label, instead of relying on it being the first subview of the view you're working from.Chaos
With old compilers you got ugly warnings if not casting.Headsail

© 2022 - 2024 — McMap. All rights reserved.