Search code examples
iphoneiosobjective-ccore-animationuilabel

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


Solution

  • 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.