myles myles - 1 year ago 107
iOS Question

Animation of setAlpha for UILabel not working

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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download