动画中断时未调用UIView动画完成块

时间:2016-04-07 07:57:10

标签: objective-c uiview core-animation uiviewanimation caanimation

我试图在这个动画结束时调用一个方法,但在某些情况下,例如,当用户离开应用程序时,它的完成块永远不会被调用。 此外,当VC与UIView动画同时出现动画时,永远不会调用完成块。

即使动画以某种方式中断,我该怎样做才能确保调用回调? 我根本不应该使用UIView动画完成块而是使用其他东西吗?请赐教..!

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

1 个答案:

答案 0 :(得分:0)

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.