0

私は iPhone カード ゲームを構築しています。各ラウンドの終了時にプレーヤーのカードをテーブルからアニメーション表示したいと考えています。プレーヤーは各ラウンドの最後に任意の数のカードを持つことができるため、静的な方法でアニメーション コードをネストすることはできません。テーブルから 2 つのカード ビュー オブジェクトをアニメーション化する次のコードがあるとします。

UICardView * __block card1 = [[UICardView alloc] init];
UICardView * __block card2 = [[UICardView alloc] init];
[UIView animateWithDuration:1.0f 
                      delay:0.0f 
                    options:UIViewAnimationCurveLinear 
                 animations:^{
                                card1.frame = CGRectOffset(cardView.frame, 0.0f, -300.0f);
                             } 
                 completion:^(BOOL finished) {
                                [UIView animateWithDuration:1.0f
                                                      delay:0.0f 
                                                    options:UIViewAnimationCurveLinear 
                                                 animations:^{
                                                                 card2.frame = CGRectOffset(cardView.frame, 0.0f, -300.0f);
                                                             } 
                                                 completion:nil]
                 }];

...NSOrderedList にある不明な数のカード ビュー オブジェクトをアニメーション化するようにコードを構成するにはどうすればよいですか?

あなたの知恵に感謝します!

4

2 に答える 2

1
-(void)animateCards:(NSMutableArray *)cards
{
    if(cards.count) {
        UICardView *cardView = [cards lastObject];
        [UIView animateWithDuration:1.0f 
                              delay:0.0f 
                            options:UIViewAnimationCurveLinear 
                         animations:^{
                             cardView.frame = CGRectOffset(cardView.frame, 0.0f, -300.0f);
                         } 
                         completion:^(BOOL finished) {
                             [cards removeLastObject];
                             [self animateCards:cards];
                         }
    } else {
        NSLog("Finished animating cards!");
    }
}

UICardViewの配列を使用してanimateCardsを呼び出すことができます。(配列は最後に空になるので、必ずコピーを作成してください)

たとえば、アニメーション化するUICardViewの配列としてself.playerCardsがある場合は、このように呼び出します。

NSMutableArray *cardsToBeAnimated = [NSMutableArray arrayWithArray:self.playerCards];
[self animateCards:cardsToBeAnimated];
于 2013-01-10T23:15:30.230 に答える
0

...または再帰、おそらく:

- (void)animateCardNTimes:(int)times
{
    if (times <= 0) return;

    __block CardView *cv = [[CardView alloc] init]; 
    [UIView animateWithDuration:1.0f 
                          delay:0.0f 
                         options:UIViewAnimationCurveLinear 
                      animations:^{
                          cv.frame = CGRectOffset(cardView.frame, 0.0f, -300.0f);
                      } 
                      completion:^(BOOL finished) {
                          [self animateCardNTimes:times - 1];
                      }
    ];
}
于 2013-01-10T23:04:15.497 に答える