由于动画循环,UIViewController不会释放

时间:2013-12-17 12:26:01

标签: ios core-animation nsoperation

我有一个执行简单动画的方法,然后再次调用自己。问题是UIViewController永远不会dealloc',我假设是因为连续运行循环。

如果我注释掉了回忆动画方法的那一行,那么dealloc就可以了。

我尝试使用performSelector来循环动画,但在[NSOperation cancelPreviousPerformRequestsWithTarget:self];中调用viewWillDisappear似乎并未取消它。可能是因为我正在使用afterDelay:0.0

这是我的动画方法:

- (void)animateArrows
{
    UIImage *toImage;
    switch (animState) {
        case ARROWS:
            toImage = arrow_1;
            animState++;
            break;

        case ARROW_1:
            toImage = arrows;
            animState++;
            break;

        case ARROWS_1:
            toImage = arrow_2;
            animState++;
            break;

        case ARROW_2:
            toImage = arrows;
            animState++;
            break;

        case ARROWS_2:
            toImage = arrow_3;
            animState++;
            break;

        case ARROW_3:
            toImage = arrows;
            animState = ARROWS;
            break;

        default:
            break;
    }

    [UIView transitionWithView:self.sendButton
                      duration:0.6f
                       options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowUserInteraction
                    animations:^{
                        [self.sendButton setImage:toImage forState:UIControlStateNormal];
                    } completion:^(BOOL finished){

                        [self performSelector:@selector(animateArrows) withObject:nil afterDelay:0.0];
//                        [self animateArrows];
                    }];
}

1 个答案:

答案 0 :(得分:2)

您需要做的就是添加一个BOOL属性,指示动画是否应该保持循环。然后将该动画块更改为:

[UIView transitionWithView:self.sendButton
                  duration:0.6f
                   options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowUserInteraction
                animations:^{
                    [self.sendButton setImage:toImage forState:UIControlStateNormal];
                } completion:^(BOOL finished){
                    if (self.repeatAnimations)
                    {
                        [self performSelector:@selector(animateArrows) withObject:nil afterDelay:0.0];
                    }
                }];

您需要执行此检查的原因是您有一个保留self的无限循环。离开此循环后,您的视图控制器将为dealloc'd。