【问题标题】:UIView animation completion block not called when animation is interrupted动画中断时未调用 UIView 动画完成块
【发布时间】:2016-04-07 07:57:10
【问题描述】:

我试图在动画结束时调用一个方法,但在某些情况下,例如,当用户离开应用程序时,它的完成块永远不会被调用。 此外,当 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];
                                              }
                                          }
                          ];
                     }
                 }
 ];}

【问题讨论】:

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


    【解决方案1】:

    您可以做的是改用CATransaction

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

    如果动画完全中断,则几乎立即调用完成块。当动画不间断地完成时,也会调用该块。

    P.S:CATransaction 适用于所有 UIView 动画。只要你在动画发生之前说begin,在动画代码之后说commit

    【讨论】:

    • 嗯,如果我有一个“链式”动画,并且在我编辑的问题中一个接一个地包含多个动画,该怎么办?
    • 然后在最后一个动画代码后面加上commit block。
    • 嗯,但我认为我必须使用 CAKeyframeAnimation,因为我正在做的正是需要这样做。 CAKeyframeAnimation 是否还确保在动画中断时立即调用其完成块,还是我必须与 CATransition 一起使用它......?
    • 我没用过CAKeyframeAnimation。但我怀疑它的行为方式可能相同。
    • 我刚刚得知这个答案是完全错误的。 CATransaction 不适用于简单的 UIView 动画块。对于整个事情,您必须一直使用 CAAnimation 的子类。
    猜你喜欢
    • 2014-09-16
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2013-02-13
    • 1970-01-01
    相关资源
    最近更新 更多