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

时间:2013-03-04 00:19:51

标签: ios objective-c-blocks uiviewanimation

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

2 个答案:

答案 0 :(得分: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];
                 }
 ];

答案 1 :(得分: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];
    };
}