Ios 堆叠UIView动画和块的正确方法?

Ios 堆叠UIView动画和块的正确方法?,ios,objective-c-blocks,uiviewanimation,Ios,Objective C Blocks,Uiviewanimation,我只使用过UIView动画和块,基本上是一步动画。我想把几个步骤叠成一个序列。下面的代码似乎正常工作,但我想知道这是否是正确的方法,以及/或者在块中放置块是否存在任何问题/限制 虽然代码格式变得有些笨拙/不可读,但块看起来很棒 CGRect newFrame = CGRectMake(0, 0, 500, 500); UIView *flashView = [[UIView alloc] initWithFrame:newFrame]; flashView.tag = 999; [flashV

我只使用过UIView动画和块,基本上是一步动画。我想把几个步骤叠成一个序列。下面的代码似乎正常工作,但我想知道这是否是正确的方法,以及/或者在块中放置块是否存在任何问题/限制

虽然代码格式变得有些笨拙/不可读,但块看起来很棒

CGRect newFrame = CGRectMake(0, 0, 500, 500);
UIView *flashView = [[UIView alloc] initWithFrame:newFrame];

flashView.tag = 999;
[flashView setBackgroundColor:[UIColor grayColor]];
[flashView setAlpha:0.f];
[self.view addSubview:flashView];

[UIView animateWithDuration:.2f
                 animations:^{
                     // STEP 1: FADE IN
                     [flashView setAlpha:1.f];
                 }
                 completion:^(BOOL finished){
                     [UIView animateWithDuration:.9f
                                      animations:^{
                                          // STEP 2: FADE OUT
                                          [flashView setAlpha:0.f];
                                      }
                                      completion:^(BOOL finished){
                                          // STEP 3: CLEAN UP
                                          [flashView removeFromSuperview];
                                      }
                      ];
                 }];

如果只使用简单的代码嵌套一次,那么这种方式并不可怕。如果要做更复杂的事情,可以尝试
animateWithDuration:delay:options:animations:completion:
使用
延迟:
作为链接动画的方式。例如:

[UIView animateWithDuration:.2f delay:0
                    options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     // STEP 1: FADE IN
                     [flashView setAlpha:1.f];
                 }
                 completion:nil
 ];
[UIView animateWithDuration:.9f delay:.2f
                    options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     // STEP 2: FADE OUT
                     [flashView setAlpha:0.f];
                 }
                 completion:^(BOOL finished){
                     // STEP 3: CLEAN UP
                     [flashView removeFromSuperview];
                 }
 ];

你所做的是正确的。有时筑巢会变得丑陋。可读性的另一种选择是将每个动画放在其自己的方法中:

-(IBAction)someButtonPressed:(id)sender {
    [self saveSomeData];
    [self fadeInAndOut];
}

-(void)fadeInAndOut {
    [UIView animateWithDuration:.2f
                     animations:^{
                         // STEP 1: FADE IN
                         [self.flashView setAlpha:1.f];
                     }
                     completion:[self fadeOut]
     ];
}

-(void (^)(BOOL))fadeOut {
    return ^(BOOL finished) {
        [UIView animateWithDuration:.9f
                         animations:^{
                             // STEP 2: FADE OUT
                             [self.flashView setAlpha:0.f];
                         }
                         completion:[self cleanUpFlashView]
         ];
    };
}

-(void (^)(BOOL))cleanUpFlashView {
    return ^(BOOL finished){
        // STEP 3: CLEAN UP
        [self.flashView removeFromSuperview];
    };
}

如果只修改一个视图,则可以查看。