durazno
durazno

Reputation: 569

UIView animation completion block not called when animation is interrupted

I'm trying to call a method whenever this animation ends but in some occasions for example, when user leaves the app, its completion block never gets called. Also when a VC appears with its animation at the same time as the UIView animation, the completion block never gets called.

What should I do to make sure the callback is called even if the animation is interrupted somehow? Should I not use UIView animation completion block at all and use something else instead? Please enlighten me..!

-(void)action {

[UIView animateWithDuration:0.3
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{

                     self.doorLeft.frame = CGRectMake(0, 0, self.doorLeft.frame.size.width, self.doorLeft.frame.size.height);
                     self.doorRight.frame = CGRectMake(self.frame.size.width -self.doorRight.frame.size.width, 0, self.doorRight.frame.size.width, self.doorRight.frame.size.height);

                 } completion:^(BOOL finished){
                     if (finished) {
                         switch (self.type) {
                             case 0:
                                 [self.delegate startTOship];
                                 break;
                             case 1:
                                 [self.delegate gameTOship];
                                 break;
                             case 2:
                                 [self.delegate shipTOgame];
                                 break;
                             case 3:
                                 [self.delegate shipTOmap];
                                 break;
                             case 4:
                                 [self.delegate gameTOmap];
                                 break;
                             case 5:
                                 [self.delegate mapTOgame];
                                 break;
                             case 6:
                                 [self.delegate mapTOship];
                                 break;
                         }

                         [UIView animateWithDuration:0.3
                                               delay:0.5
                                             options:UIViewAnimationOptionCurveEaseOut
                                          animations:^{

                                              self.doorLeft.frame = CGRectMake(-self.doorLeft.frame.size.width, 0, self.doorLeft.frame.size.width, self.doorLeft.frame.size.height);
                                              self.doorRight.frame = CGRectMake(self.doorRight.frame.size.width *2, 0, self.doorRight.frame.size.width, self.doorRight.frame.size.height);

                                          } completion:^(BOOL finished){
                                              if (finished) {

                                                  [self actionEnded];
                                              }
                                          }
                          ];
                     }
                 }
 ];}

Upvotes: 0

Views: 1629

Answers (1)

Robert J. Clegg
Robert J. Clegg

Reputation: 7370

What you can do, is use CATransaction instead.

[CATransaction begin];

[CATransaction setCompletionBlock:^{
    [self actionEnded]
}];

[UIView animateWithDuration:8.3 delay:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{

    self.doorLeft.frame = CGRectMake(-self.doorLeft.frame.size.width, 0, self.doorLeft.frame.size.width, self.doorLeft.frame.size.height);
    self.doorRight.frame = CGRectMake(self.doorRight.frame.size.width *2, 0, self.doorRight.frame.size.width, self.doorRight.frame.size.height);

} completion:nil];

[CATransaction commit];

If the animation is interrupted at all, the completion block is called almost immediately The block is also called when animation completes uninterrupted.

P.S: CATransaction works with all UIView animations. As long as you say begin before the animation happens and commit after the animation code.

Upvotes: 1

Related Questions