2016-08-02 10 views
0

UIViewアニメーションが終了したらアクションを実行します。UIView beginAnimationsが終了した後でアクションを実行する

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 
}]; 

を使用して試してみました。しかし、それは仕事を得ることはありません。

更新コード ボタンは2つのアクションに接続されています。

- (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

に役立つかもしれません。 – Abizern

答えて

1

このコードは、UIViewのの `beginAnimations`があなたの代わりにブロックベースのアニメーションを使用する必要があり、iOS4をより新しいもののために推奨されます

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

ありがとうございました。私はあなたのコードを試しました。しかし、継続時間は働かない。フリップアニメーションは速くなります。私はanimateWithDuration:2.0を変更しようとしました。私は何を間違えますか? – user3266053

+0

@ user3266053あなたのコードベースを共有できますか? –

+0

私はコードを更新しました。 – user3266053

関連する問題