0

I want to do several animations in series within a loop. Each animation does "UI animation" first followed by "completion block". My question is how to add several such animations in series within a loop so that the executing sequence follows: conditon->"UI animation 1" -> "completion block 1" -> condition-> "UI animation 2" -> "completion block 2" ->...-> condition->"UI animation n" -> "completion block n".

 while(condition){
    [UIView animateWithDuration:0.3 animations:^{
        //UI animation
        CGRect frame = sourceView.frame;
        frame.origin = destPosition;
        view.frame = frame;
    } completion:^(BOOL finished) {
        //completion block
        self.currentBoard = destBoard;
    }];
 }

sorry, I may not present my question clearly. I have updated my question as above, a while loop is added. The issue is that I find next condition test is executed before the completion block of previous animation

Johnson
  • 491
  • 1
  • 3
  • 13

1 Answers1

0

You need to start UI Animation N in the completion block N-1.

[UIView animateWithDuration:0.3 animations:^{
  // UI Animation 1
} completion:^(BOOL finished) {
  // Handle the completion of UI Animation 1

  // Start the next animation
  [UIView animateWithDuration:0.3 animations:^{
    // UI Animation 2
  } completion:^(BOOL finished) {
    // Completion block of animation 2
  }
}
  • the nesting is difficult if n is large. Is it possible to achieve within a loop? – Johnson Mar 29 '17 at 13:08
  • You can look at this solution where the animations are put into an array and then looped through. http://stackoverflow.com/questions/17949511/the-proper-way-of-doing-chain-animations – Steffen Lund Andersen Mar 29 '17 at 13:12
  • Thanks for your information. Actually I have tried that before I post my question. The problem of that solution is no where to put the completion block I want. The completion block in that solution is only to trigue next animation. – Johnson Mar 29 '17 at 13:15
  • You can just put in your own completion blocks that does some custom work before continuing the cycle by calling getNextAnimation()(completed); – Steffen Lund Andersen Mar 29 '17 at 13:28
  • According to your suggestion,I have realize that by modifying block signature. Now I want add condition test in a while loop with animation. However,next condition test is executed before the previous animation completion block as in my updated question description – Johnson Mar 29 '17 at 23:09