-1

I want to animate uilabel to change text every 1 second.
But when i run the app, it directly shows the last word. I.e. i have 4 different sentences to display one after another but it directly shows the last one.


This is the code that I am using

[UIView animateWithDuration:1.0
                     animations:^{
                             self.mapStat.alpha = 0.0f;
                             self.mapStat.text = @"Retriving Coordinates";;
                             self.mapStat.alpha = 1.0f;
                     }];
    [UIView animateWithDuration:1.0
                     animations:^{
                         self.mapStat.alpha = 0.0f;
                         self.mapStat.text = @"Retrieved Coordinates";
                         self.mapStat.alpha = 1.0f;
                     }];
    [UIView animateWithDuration:1.0
                     animations:^{
                         self.mapStat.alpha = 0.0f;
                         self.mapStat.text = @"Applying Coordinates";
                         self.mapStat.alpha = 1.0f;
                     }];
    [UIView animateWithDuration:1.0
                     animations:^{
                         self.mapStat.alpha = 0.0f;
                         self.mapStat.text = @"Loading Map!";
                         self.mapStat.alpha = 1.0f;
                     }];

What is wrong with the code?
And is there any easier way to perform this animation?

votelessbubble
  • 788
  • 6
  • 19

3 Answers3

2

Nothing is wrong with your code. What's wrong is that you, instead of reading the documentation, assumed that the text property of UILabel can be animated. It can't.


If you want to fade text in and out, you can use the animateWithDuration:animations:completion: method of UIView and change the text in the completion block, after the label has faded out.

  • This answer, while correct, doesn't address the OP's desire to have one sentence fade into another sentence. This can be accomplished using CATransition: http://stackoverflow.com/a/6267259/1445366 – Aaron Brager Dec 21 '13 at 12:21
  • @AaronBrager The second part of the answer addresses exactly that, maybe you misinterpreted it. –  Dec 21 '13 at 12:26
  • Your solution would fade one out, then another in. I believe the OP is looking for a cross-dissolve. – Aaron Brager Dec 21 '13 at 12:52
  • @AaronBrager Aham. In this case, should the q/a you linked to be considered a duplicate? –  Dec 21 '13 at 12:55
1

The Problem is you not set the delay for next animation in your code,you set delay time for complete previous animations,like this

[UIView animateWithDuration:1.0f
                          delay:0.0f
                        options:UIViewAnimationOptionAutoreverse
                     animations:^
     {
         self.mapStat.alpha = 0.0f;
         self.mapStat.text = @"Retriving Coordinates";
         self.mapStat.alpha = 1.0f;
     }
    completion:^(BOOL finished)
     {
    }];
    [UIView animateWithDuration:1.0f
                          delay:1.0f
                        options:UIViewAnimationOptionAutoreverse
                     animations:^
     {
         self.mapStat.alpha = 0.0f;
         self.mapStat.text = @"Retriving Coordinates";
         self.mapStat.alpha = 1.0f;
     }
                     completion:^(BOOL finished)
     {
     }];
    [UIView animateWithDuration:1.0f
                          delay:2.0f
                        options:UIViewAnimationOptionAutoreverse
                     animations:^
     {
         self.mapStat.alpha = 0.0f;
         self.mapStat.text = @"Applying Coordinates";
         self.mapStat.alpha = 1.0f;
     }
                     completion:^(BOOL finished)
     {
     }];
    [UIView animateWithDuration:1.0f
                          delay:2.0f
                        options:UIViewAnimationOptionAutoreverse
                     animations:^
     {
         self.mapStat.alpha = 0.0f;
         self.mapStat.text = @"Loading Map!";
         self.mapStat.alpha = 1.0f;
     }
                     completion:^(BOOL finished)
     {
     }];
NANNAV
  • 4,875
  • 4
  • 32
  • 50
0

Try this

int NoOfStringsLeft =  4;

[NSTimer scheduledTimerWithTimeInterval:1.0
        target:self
        selector:@selector(targetMethod:)
        userInfo:nil
        repeats:Yes];

-(void)targetMethod:(NSTimer *)timer
{
    //Update label text

     NoOfStringsLeft --;

     if(NoOfStringsLeft == 0)
     {
         [timer invalidate];
     }      
}
Aditya Deshmane
  • 4,676
  • 2
  • 29
  • 35