1

So i was trying to animate label which will start with a text then after certain time it will fade and new text comes to the screen which will fade after certain time and then new text comes. I want this to happen recursively. I need to continue animating for 15 seconds , the again same thing happens for next 15 seconds.

- (void)viewDidLoad {
    [super viewDidLoad];

    [self MyLabelAnimation];

}

- (void)MyLabelAnimation {
    self.myLabel.text = @"Text 1";
    [UIView animateWithDuration:0.3 animations:^{
        self.myLabel.alpha = 1.0;
    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.myLabel.alpha = 0.0;
        } completion:^(BOOL finished) {

            self.myLabel.text = @"Text 2";
            [UIView animateWithDuration:0.3 animations:^{
                self.myLabel.alpha = 1.0;
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    self.myLabel.alpha = 0.0;
                } completion:^(BOOL finished) {

                    self.myLabel.text = @"Text 3";
                    [UIView animateWithDuration:0.3 animations:^{
                        self.myLabel.alpha = 1.0;
                    } completion:^(BOOL finished) {

                        [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                            self.myLabel.alpha = 0.0;
                        } completion:^(BOOL finished) {

                            self.myLabel.text = @"Text 4";
                            [UIView animateWithDuration:0.3 animations:^{
                                self.myLabel.alpha = 1.0;
                            } completion:^(BOOL finished) {
                                [UIView animateWithDuration:0.0 delay:4.8 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                                    self.myLabel.alpha = 0.0;
                                } completion:^(BOOL finished) {

                                    [self MyLabelAnimation];
                                }];
                            }];
                        }];
                    }];
                }];
            }];
        }];
    }];
}

So Here i am calling mylabel animation from viewdidload and then i called the method at the end of the completion block of 4th animation.

Here everything is working as expected but the last animation to first animation transition is not smooth as other transition. Is it because i missed something . I really am not able to figure out why is it happening.

Secondly, Is is the right way to get the animation like this. Or is there a better way to do this ?

Sudhanshu Gupta
  • 2,255
  • 3
  • 36
  • 74

2 Answers2

3

You might find something like this a bit easier to manage:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.counter = 0;
    self.animations = @[
                        @{@"Text":@"Hello", @"Duration":@(2.7)},
                        @{@"Text":@"Text 1", @"Duration":@(2.7)},
                        @{@"Text":@"Text 2", @"Duration":@(2.7)},
                        @{@"Text":@"Text 3", @"Duration":@(2.7)},
                        @{@"Text":@"Text 4", @"Duration":@(4.8)},
                        ];

    [self animate:0];
}


- (void)animate:(int)counter {
    self.counter = counter % self.animations.count;

    NSDictionary *item = self.animations[self.counter];

    self.myLabel.alpha = 0;
    self.myLabel.text = item[@"Text"];

    [UIView animateWithDuration:0.3
                     animations:^{
                         self.myLabel.alpha = 1.0;
                     } completion:^(BOOL finished) {
                         [UIView animateWithDuration:0.3
                                               delay:[item[@"Duration"] floatValue]
                                             options:UIViewAnimationOptionCurveEaseInOut
                                          animations:^{
                                              self.myLabel.alpha = 0.0;
                                          } completion:^(BOOL finished) {
                                              [self animate:++self.counter];
                                          }];
                     }];
}
norders
  • 1,160
  • 9
  • 13
  • do you know how can i pause animation when app goes background ? – Sudhanshu Gupta Oct 04 '16 at 05:26
  • Add a global `BOOL` to the class called `shouldStopAnimation`. Check the value at the start of the `animate` function. If it's set, then return immediately. Add an observer for the ApplicationWillEnterBackground notification and set the bool value to `YES` when received. You can also restart the animation when coming to the foreground by setting it no `NO` and re-calling `animate:self.counter`. – norders Oct 04 '16 at 14:02
  • Don't forget to mark an answer as correct if it solved your problem. – norders Oct 05 '16 at 11:34
0

Try this code....

 - (void)viewDidLoad {
    [super viewDidLoad];

     [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(MyLabelAnimation:) userInfo:nil repeats:NO];

}
- (void)MyLabelAnimation:(NSTimer*) timer {
    self->mylabel.text = @"Hello";
    [UIView animateWithDuration:0.3 animations:^{
        self->mylabel.alpha = 1.0;
    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self->mylabel.alpha = 0.0;
        } completion:^(BOOL finished) {

            self->mylabel.text = @"Text 2";
            [UIView animateWithDuration:0.3 animations:^{
                self->mylabel.alpha = 1.0;
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    self->mylabel.alpha = 0.0;
                } completion:^(BOOL finished) {

                    self->mylabel.text = @"Text 3";
                    [UIView animateWithDuration:0.3 animations:^{
                        self->mylabel.alpha = 1.0;
                    } completion:^(BOOL finished) {

                        [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                            self->mylabel.alpha = 0.0;
                        } completion:^(BOOL finished) {

                            self->mylabel.text = @"Text 4";
                            [UIView animateWithDuration:0.3 animations:^{
                                self->mylabel.alpha = 1.0;
                            } completion:^(BOOL finished) {
                                [UIView animateWithDuration:0.0 delay:4.8 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                                    self->mylabel.alpha = 0.0;
                                } completion:^(BOOL finished) {

                                     [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(MyLabelAnimation:) userInfo:nil repeats:NO];
                                }];
                            }];
                        }];
                    }];
                }];
            }];
        }];
    }];

}
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49