uiview动画完成块中的递归会导致堆栈溢出吗?

时间:2012-09-11 07:37:56

标签: objective-c cocoa-touch uiview uiviewanimation

我想做一个无休止的动画(例如,让视图无休止地在两个位置之间移动)

我想知道这是否是一个递归,是否会导致堆栈溢出? pesudo代码:

-(void)doAnimation {
    [UIView animateWithDuration:1.0
            delay:0.0
            options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction)
            animations:^{
                if(view is at Position A){ 
                    set frame to Postion B;
                }else{
                    set frame to Positon A;
                }
            }
            completion:^(BOOL finished){
                [self doAnimation];
            }
     ];
}

提前致谢

利奥

2 个答案:

答案 0 :(得分:4)

当你的完成被调用时,调用堆栈已经被解除到主运行循环,所以,不,你不会得到任何堆栈溢出,只是一些无限的动画。

答案 1 :(得分:1)

这是执行重复动画的错误方法。查看动画选项UIViewAnimationOptionRepeatUIViewAnimationOptionAutoreverse代替(documentation, see Constants at the end)您的动画可以重写如下:

-(void)doAnimation {
    [UIView animateWithDuration:1.0
            delay:0.0
            options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse)
            animations:^{
                set frame to Position B;
                }
            }
            completion:nil
            }
     ];
}

假设您的观点从A位开始。