Animating Labels with delay and duration Ios

2019-07-27 00:21发布

问题:

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 ?

回答1:

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];
                                          }];
                     }];
}


回答2:

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];
                                }];
                            }];
                        }];
                    }];
                }];
            }];
        }];
    }];

}