2016-08-02 176 views
0

我想在UIView動畫完成時執行動作。在UIView開始動畫後完成動作完成

if (flipStateHav1 == 1 && btnFrontHav1.tag == 1) { 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.8]; 

    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:buttonContainerHav1 cache:YES]; 
    [self.buttonContainerHav1 bringSubviewToFront:btnBackHav1]; 

    [UIView commitAnimations]; 

    //It is this code below I want to run after the animation is finish. 
    buttonContainerHav1.userInteractionEnabled = YES; 
    flipTurns = 0; 
    flipStateHav2 = 0; 
    par = 0; 
} 

我一直在使用

completion:^(BOOL finished){ 
    // your code 
}]; 

嘗試,但沒有得到它的工作,可能是我做錯了什麼。

更新代碼 該按鈕連接到兩個操作。

- (IBAction) buttonPressedFlipHav1 { 

if (flipTurns == 0 || flipTurns == 1) { 

    flipTurns += 1; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.8]; 

     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:buttonContainerHav1 cache:YES]; 
     [self.buttonContainerHav1 bringSubviewToFront:btnFrontHav1]; 
     buttonContainerHav1.userInteractionEnabled = NO; 
     flipStateHav1 = 1; 
     btnFrontHav1.tag = 1; 

     autoFlipCount += 1; 

     if (flipStateHav1 == 1 && flipStateHav2 == 1) { 
      par = 1; 
      parHav = 1; 
      btnFrontHav1.tag = 0; 
      btnFrontHav2.tag = 0; 

      flipTurns = 0; 
      autoFlipCount = 0; 

      if (parHav == 1 && parRiddare == 1 && parSkold == 1) { 

       autoFlipTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(autoFlipAll) userInfo:nil repeats:NO]; 

      } 
     } 

    } completion:^(BOOL finished) { 

    NSLog(@"autoFlipCount = %d",autoFlipCount); 
    NSLog(@"Flipturns = %d",flipTurns); 
    }]; 

    } 

} 

而且它也連接到這個動作。

- (IBAction) autoFlip { 
     autoFlipTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(callAfterFiveSecond) userInfo:nil repeats:NO]; 
} 

5秒後它跳到這個if語句。 更新

if (flipStateHav1 == 1 && btnFrontHav1.tag == 1) { 

    [UIView animateWithDuration:0.8 animations:^{ 

     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:buttonContainerHav1 cache:YES]; 
     [self.buttonContainerHav1 bringSubviewToFront:btnBackHav1]; 

    } completion:^(BOOL finished) { 
     buttonContainerHav1.userInteractionEnabled = YES; 
     flipTurns = 0; 
     flipStateHav1 = 0; 
     par = 0; 
    }]; 

} 
+0

的UIView的'beginAnimations'氣餒比iOS4的任何更新,你應該使用基於塊的動畫代替。 – Abizern

回答

1

此代碼可以幫助你

[UIView animateWithDuration:0.4 animations:^{ 
    //animation code here` 
} completion:^(BOOL finished) { 
     //code after animation here 
}]; 
+0

謝謝你的回答。我確實嘗試了你的代碼。但持續時間不起作用。翻轉動畫變得快速。我試圖改變animateWithDuration:2.0。我做錯了什麼。 – user3266053

+0

@ user3266053你能分享你的代碼庫嗎? –

+0

我已更新代碼。 – user3266053