具有延迟和持续时间Ios的动画标签

时间:2016-10-03 06:33:14

标签: ios objective-c iphone animation app-store

所以我试图制作一个以文字开头的标签,然后在一定时间后它会褪色,新的文字会出现在屏幕上,这会在一段时间后消失然后新的文字出现。我希望这是递归发生的。我需要继续动画15秒,接下来的15秒再次发生同样的事情。

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

所以这里我从viewdidload调用mylabel动画,然后我在第4个动画的完成块结束时调用了该方法。

这里的一切都按预期工作,但第一个动画过渡的最后一个动画是不顺畅作为其他过渡。是因为我错过了什么。我真的无法弄清楚为什么会这样。

其次,Is是获得这样动画的正确方法。或者有更好的方法吗?

2 个答案:

答案 0 :(得分:3)

您可能会发现这样的事情更容易管理:

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

答案 1 :(得分:0)

试试这个代码......

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

}
相关问题