Animation of setAlpha for UILabel not working

1.7k views Asked by At

I have some UILabels on a view that I want to fade when the someone is holding down on the view.

I am trying to perform the fade by changing the alpha value of the text from 1.0 to 0.5 and then back again when the touch is released.

Here is my code:

[UIView transitionWithView:self.view duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations: ^ {
        [someImageView removeFromSuperview];
        [[self someText] setAlpha:0.5];
    } completion:nil];

The imageView that is being removed within this block is animating fine, however, the alpha change of the text doesn't animate, it just happens instantly once the animation is finished, i.e. while the imageView animates and is removed the text remains at alpha 1.0, then when the animation is completed, the text alpha suddenly changes from 1.0 to 0.5 with no animation.

Am I doing this wrong or is there some other way I should be doing this?

The only other way I can think to do it is to duplicate the UILabel so that there are two, one with alpha 1.0 and one with alpha 0.5. I could then fade to hidden the original UILabel and fade in the translucent UILabel but this seems like a hideous solution.

3

There are 3 answers

5
architectpianist On BEST ANSWER

Quick fix: Use an animateWithDuration method instead of transitionWithView for alpha transitions.

4
Sergii Martynenko Jr On

Problem is that textColorproperty isn't animatable. If your label has different colour from its underlaying view, you would see that it becomes transparent while animation takes place. To animate change of text colour you should use CATextLayer instead of your UILabel. Than you can animate change of it foregroundColour property.

Here is a good tutorial on this one http://corecocoa.wordpress.com/2011/10/04/animatable-text-color-of-uilabel/

0
Sagi Mann On

The only solution that worked for me was to place the UILabel inside a UIView & animate the alpha (or only the bg color, depends on what you need) of the UIView instead of the UILabel's.