UIView动画 - Objective-C。这个动画会导致堆栈溢出异常吗?

时间:2014-05-21 17:33:50

标签: ios objective-c animation uiview objective-c-blocks

我有一个带有三个动画块的方法,它可以动画两个UIImageViews。这段代码将来是否会导致堆栈溢出异常?

我的意思是递归块。我需要按顺序为它们制作动画。第一个大图像应该淡入,然后一个较小的图像应该淡入,然后两个图像应该淡出,动画应该重新开始。

- (void)startAnimation {

    [UIView animateWithDuration:0.3 animations:^{
        [_bigImage setAlpha:1];
    }
    completion:^(BOOL finished) {

        [UIView animateWithDuration:0.3 animations:^{
            [_mediumImage setAlpha:1];
        }
        completion:^(BOOL finished1) {

            [UIView animateWithDuration:0.2 animations:^{
                [_mediumImage setAlpha:0];
                [_bigImage setAlpha:0];
            }
            completion:^(BOOL finished3) {

              [self startAnimation];
            }];
        }];
    }];
}

1 个答案:

答案 0 :(得分:1)

您发布的内容应该可以正常使用。是的,你也可以使用CAAnimations来做,但那些更难理解。

我不同意dasdom。我认为你目前的做法是合理的。

我确实发现嵌套的animateWithDuration:方法难以弄清楚。它很容易迷失在嵌套块语法中。

相反,您可以添加一个整数或枚举实例变量来跟踪动画步骤,让您的方法为每个步骤调用不同的动画代码,然后在每个动画块中再次调用自身:

在标题中:

typedef enum 
{
  first,
  second,
  third,
  done
} animationSteps

@interface MyClass
{
  animationSteps currentStep
}

.m文件:

- (void)doAnimation 
{

  [UIView animateWithDuration:0.3 animations:^
  {
    switch (currentStep)
    {
      case first:
        [_bigImage setAlpha:1];
        break;
      case second:
        [_mediumImage setAlpha:1];
        break;
      case third:
        [_mediumImage setAlpha:0];
        [_bigImage setAlpha:0];
        break;
    }
  } 
  completion:^(BOOL finished) 
  {
    currentStep++;
    if (currentStep == done)
      currentStep = first;
    [self doAnimation];
  }
  ]; 
}
相关问题