代码之家  ›  专栏  ›  技术社区  ›  spring

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

  •  3
  • spring  · 技术社区  · 12 年前

    我只使用过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 回复  |  直到 12 年前
        1
  •  1
  •   Christopher Pickslay    12 年前

    你所做的是正确的。有时筑巢会变得很难看。可读性的一种替代方法是将每个动画放在自己的方法中:

    -(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];
        };
    }
    
        2
  •  1
  •   Rich Schonthal    12 年前

    如果您只使用简单的代码嵌套一次,那么这种方式并不可怕。如果你要做一些更复杂的事情,你可以试试 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];
                     }
     ];