2012-03-07 5 views
0

アニメーションの配列を1つずつ内部関数に渡してお互いにアニメーションを入れ子にする必要はありません。お互いの完了ブロック。だから、私はこれをテストするための少しの方法を書いたし、どうしたのか、それは地獄のようにクラッシュする。しかし、なぜ私は理解していない。 、私はそれをデバッグする場合アニメーションの配列を渡すとアプリケーションがクラッシュする

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

、それは親切にすべての私のアニメーションを通過その完了ブロックで、最終的なアニメーションを呼び出して、クラッシュ:私はこのように、このアニメーションを渡し

+(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]; 
} 

:これは私の方法であり、致命的。私はここで間違って何をしていますか?

+1

「クラッシュ致命的」はあまり具体的ではありません。何が起こるのですか? – Jim

答えて

1

問題があります。-addObject:NSMutableArrayは、追加されたオブジェクトを保持しますがコピーしません。ブロックを宣言すると、ブロックがスタックにあり、スコープの最後に破棄されます。ヒープにするには、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); 
+0

私はそのようなものだと思った。ありがとう! – Fuggly

関連する問題