0

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.

myles
  • 1,681
  • 1
  • 15
  • 27
  • 1
    Have you tried using `animateWithDuration` instead of `transitionWithView`? – architectpianist Jun 18 '15 at 14:46
  • Just tried this now: `[UILabel animateWithDuration:0.3 animations: ^ { [[self someText] setAlpha:0.5]; } completion:nil];` but still has the same issue. Is this how you meant? – myles Jun 18 '15 at 14:51

3 Answers3

1

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/

Sergii Martynenko Jr
  • 1,407
  • 1
  • 9
  • 18
1

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

architectpianist
  • 2,562
  • 1
  • 19
  • 27
  • Can't seem to get this working consistently. I'm using the following code: `[UILabel animateWithDuration:0.3 animations: ^ { [[self someText] setAlpha:0.5]; } completion:nil];`. Is this correct? It seems to work for longer durations (> 0.9) but not for any less. – myles Jun 18 '15 at 15:00
  • I'm testing it out now and it seems to work both ways, actually. Using `self.view` as the parameter for `transitionWithView` seems to slow down the performance, but both methods work. Maybe it's your frame rate that's causing the apparent malfunction. – architectpianist Jun 18 '15 at 15:06
  • Hmm, but if that was the case surely the animation of the ImageView I am removing would also have the same issue, yet it works correctly? – myles Jun 18 '15 at 15:09
  • That's strange. I'm able to get the transitions working in the simulator just fine, removing subviews and changing alpha. I still think there might be a lag from animating multiple objects quickly, or there must be some other confounding factor. Avoid using `self.view` for the transition, and if possible use `animateWithDuration` entirely (animate the image view's alpha to 0, then remove it in the completion block). – architectpianist Jun 18 '15 at 15:27
  • Ahh great got it working. Moved everything over to the `animateWithDuration` block as you suggested and removed the `transitionWithView` (this removal was the key). With both running one after another (imageView being removed in `transitionWithView` and text alpha being changed in `animateWithDuration`) it doesn't work, seems the `transitionWithView` interferes with the `animateWithDuration`? With it all in the single `animateWithDuration` block it works fine (also using the alpha change of the imageView and removal in the completion block as you suggested). Many thanks! – myles Jun 18 '15 at 15:38
0

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.

Sagi Mann
  • 2,967
  • 6
  • 39
  • 72