【问题标题】:Application crashes when passing array of animations传递动画数组时应用程序崩溃
【发布时间】:2012-03-07 13:15:23
【问题描述】:

我认为将一组动画传递给一个一个接一个地运行所有动画的内部函数是一个绝妙的主意,这样我就不需要在彼此和彼此的完成块中嵌套动画。所以我写了一个小方法来测试它,你猜怎么着,它像地狱一样崩溃。但我不明白为什么。这是我的方法:

+(void) internalAnimateWithArrayOfAnimationBlocks:(NSArray*) animationBlocks withIndex:(NSUInteger) index withCompletionAnimation:(void (^)(BOOL finished)) completionBlock { 
  __block NSArray* newAnims = animationBlocks;
  __block NSUInteger theIndex = index;
  if (index < [newAnims count] - 1) {
    [UIView animateWithDuration:0.1 animations:^{
      void (^animBlock) (void) = [newAnims objectAtIndex:theIndex];
      animBlock();
      theIndex++;
      [RMAnimater internalAnimateWithArrayOfAnimationBlocks:newAnims withIndex:theIndex withCompletionAnimation:completionBlock];
    }];
  }
  else {
    [UIView animateWithDuration:0.1 animations:^{
      void (^animBlock) (void) = [newAnims objectAtIndex:theIndex];
      animBlock();
      theIndex++;
    } completion:completionBlock];
  }
}

+(void) animateWithArrayOfAnimationBlocks:(NSArray*) animationBlocks withCompletionAnimation:(void (^)(BOOL finished)) completionBlock { 
  [RMAnimater internalAnimateWithArrayOfAnimationBlocks:animationBlocks withIndex:0 withCompletionAnimation:completionBlock];
}

我这样传递这个动画:

NSMutableArray* animations = [NSMutableArray array];
[animations addObject:^{
  CGRect frame = theTile.textField.frame;
  frame.origin.x -= 10;
  theTile.textField.frame = frame;
}];

当我调试它时,它会检查我所有的动画,用它的完成块调用最终动画,然后致命地崩溃。我在这里做错了什么?

【问题讨论】:

  • “致命的崩溃”不是很具体。会发生什么?

标签: iphone ios ipad uiview uiviewanimation


【解决方案1】:

问题是,调用NSMutableArray-addObject: 将保留但不会复制添加的对象。当你声明一个块时,它在堆栈中,它将在作用域的末尾被销毁。要使其进入堆,您必须Block_copy 或发送copy 消息到块。因此,要解决您的问题,您必须:

NSMutableArray* animations = [NSMutableArray array];
void (^animBlock)(void) = Block_copy(^{
  CGRect frame = theTile.textField.frame;
  frame.origin.x -= 10;
  theTile.textField.frame = frame;
});
[animations addObject:animBlock];
Block_release(animBlock);

【讨论】:

    猜你喜欢
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多