2014-03-27 117 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:^{ 
}];