Alberto Juarez
Alberto Juarez

Reputation: 444

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

Upvotes: 2

Views: 5920

Answers (1)

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73698

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.

Upvotes: 12

Related Questions