Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I have text fade in to a UILabel?

Right now I have an IBAction for a button which generates a random number with each number assigned to display text in a UILabel. Rather than just appear, I would like the text to fade in or any other interesting animation would be cool too. Does anyone know a simple way to make this happen?

Right now I just use

labelMain.text = @"my text";
like image 222
Herbie999 Avatar asked Jun 28 '13 06:06

Herbie999


1 Answers

I know this is a bit stale (11 months old), but I think it's worth mentioning an alternative to the other approaches posted, which I feel is a better solution.

Make use of the UIView transitionWithView:duration:options:animations:completion class-level method, like this:

NSTimeInterval duration = 0.5f;
[UIView transitionWithView:labelMain
                  duration:duration
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                  labelMain.text = @"new value";
                } completion:nil];

This will cross-fade from the previous value of labelMain.text to "new value".

This approach works on other view values as well, such as changing the image of a UIImageView, for instance.

See Apple's docs on the topic.

like image 138
levigroker Avatar answered Sep 22 '22 17:09

levigroker