3

我只使用了一点 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];
                                      }
                      ];
                 }];
4

2 回答 2

1

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

[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];
                 }
 ];
于 2013-03-04T00:34:41.067 回答
1

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

-(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];
    };
}
于 2013-03-04T19:41:35.317 回答