【问题标题】:UIView animation - Objective-C. Will this animation cause a stack overflow exception?UIView 动画 - Objective-C。这个动画会导致堆栈溢出异常吗?
【发布时间】:2014-05-21 17:33:50
【问题描述】:

我有一个包含三个动画块的方法,它为两个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];
            }];
        }];
    }];
}

【问题讨论】:

  • 不要那样做。使用组动画。
  • @dasdom,你能告诉我怎么做吗?我需要按顺序为它们设置动画,第一个大图像应该淡入,然后较小的图像应该淡入,然后所有 2 个图像应该淡出,动画应该重新开始

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


【解决方案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];
  }
  ]; 
}

【讨论】:

  • 很酷,但是会导致stackoverflow异常?
  • 没有。 Apple 的动画方法是异步操作的。第一次调用在完成块被调用之前返回。
猜你喜欢
  • 2016-02-24
  • 2011-01-13
  • 2011-02-08
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
  • 1970-01-01
  • 2019-07-02
  • 1970-01-01
相关资源
最近更新 更多