Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show UILabel animated

I need to show a message that appears with animation and hide after a few seconds also with animation.

Does anyone know how this is possible?

Thank you very much for everything.

regards

like image 677
Alberto Juarez Avatar asked Feb 19 '26 22:02

Alberto Juarez


1 Answers

Its easy, try chaining your animations together. First fadeIn, then fadeOut. What below code does is first set alpha to 0. Then animate the appearance of the label in 1 sec. As soon as this is done, wait for 4 seconds, then start the fadeOut animation in the same manner.

[label setText:@"some text"];
[label setAlpha:0.0];
[UIView animateWithDuration:1.0 
                      delay:0 
                    options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                 animations:^(void) 
 {
     [label setAlpha:1.0];
 } 
                 completion:^(BOOL finished) 
 {
     if(finished)
     {
         [UIView animateWithDuration:1.5 
                               delay:4 
                             options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                          animations:^(void) 
          {
              [label setAlpha:0.0];
          } 
                          completion:^(BOOL finished) 
          {
              if(finished)
                  NSLog(@"Hurray. Label fadedIn & fadedOut");
          }];
     }
 }];

This way of chaining animation in iOS is one of the most effective ways to do so.

like image 168
Srikar Appalaraju Avatar answered Feb 22 '26 10:02

Srikar Appalaraju



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!