2014-03-27 91 views
0

我想实现一个嵌套的动画块。我面临的问题是只有我的完成块代码被调用。当我发表评论时,最初的部分被调用。如何让动画完成第一个动画,然后再调用第二个动画?UIView动画块问题

- (void)viewDidAppear:(BOOL)animated 
{ 
    _finalLogoCenter = self.logoImage.center; 
    // Logo Animation 
    [self logoAnimation]; 
} 

- (void) logoAnimation 
{ 

    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationOptionTransitionNone 
        animations:^ { 
         // Move Up 
         [self moveLogoUpFirst]; 
        }completion:^(BOOL finished) 
        { 
         // Move below final point 
         [self moveLogoDownBeyondFinalPoint]; 
        }]; 
} 

- (void) moveLogoUpFirst 
{ 
    __block CGPoint logoCenter = self.logoImage.center; 
    logoCenter.y = 290.0f; 
    self.logoImage.center = logoCenter; 
    [self.logoImage setAlpha:0.0f]; 
    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationOptionTransitionNone 
        animations:^ { 
         logoCenter.y = _finalLogoCenter.y - 20; 
         self.logoImage.center = logoCenter; 
         [self.logoImage setAlpha:1.0f]; 
        }completion:nil]; 
} 

- (void) moveLogoDownBeyondFinalPoint 
{ 
    __block CGPoint logoCenter = self.logoImage.center; 
    self.logoImage.center = logoCenter; 
    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationOptionTransitionNone 
        animations:^ { 
         logoCenter.y = _finalLogoCenter.y + 5; 
         self.logoImage.center = logoCenter; 
        }completion:nil]; 
} 
+0

尝试用performSelector waitUntilDone – Jasper

+0

@JasperPol谢谢。这确实有效。但我需要计算以前的动画的时间,并在afterDelay中设置它。完成块不会处理这个问题吗? – Anil

+0

好吧,你知道动画需要1秒,所以完成块在1秒后被触发。检查这个答案,它可能是一个更好的方式来动画你想要做的事情:http://stackoverflow.com/questions/1632364/shake-visual-effect-on-iphone-not-shaking-the-device/10007898# 10007898 – Jasper

回答

1

我相信问题来自另一个动画中的动画。这没有任何意义,我想这可能会导致意想不到的行为。

你可以做的是在第一动画设置完成块

- (void) moveLogoUpFirst:(void (^)(BOOL finished))completion 
{ 
    __block CGPoint logoCenter = self.logoImage.center; 
    logoCenter.y = 290.0f; 
    self.logoImage.center = logoCenter; 
    [self.logoImage setAlpha:0.0f]; 
    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationOptionTransitionNone 
        animations:^ { 
         logoCenter.y = _finalLogoCenter.y - 20; 
         self.logoImage.center = logoCenter; 
         [self.logoImage setAlpha:1.0f]; 
        }completion:^(BOOL finished) 
        { 
         completion(finished); 
        }]; 
} 

当你骂它只是使用此代码没有任何动画:

[self moveLogoUpFirst:^(BOOL finished) { 
    [self moveLogoDownBeyondFinalPoint] 
}]; 
0

按照一般的做法,你可以创建如下功能:

-(void)someFunctionWithCompletion:(void (^)(void))completionBlock{ 
    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationOptionTransitionNone 
        animations:^ { 
        }completion:^(BOOL finished) 
        { 
         if(completionBlock) 
          completionBlock(); 
        }]; 
} 

并且将其命名为:

[... someFunctionWithCompletion:^{ 
}];