2013-01-10 55 views
0

我建立一個iPhone的紙牌遊戲,我想在每一輪結束動畫的玩家牌假表。玩家在每輪結束時可以有任意數量的卡片,所以我不能以任何靜態方式嵌套動畫代碼。如果我有下面的代碼以動畫兩個證視圖對象從桌子上...嵌套數目不詳的UIView動畫

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卡片視圖的對象?

非常感謝你的智慧!

+0

也許使用'for'循環? – 2013-01-10 22:56:37

+0

@ H2CO3您能否詳細說明一下,或許可以向我展示一個例子? – BeachRunnerFred

+0

而是遞歸,請參閱我的答案。 – 2013-01-10 23:04:31

回答

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!"); 
    } 
} 

您可以致電animateCards與UICardView的數組。 (請務必進行復制,因爲該陣列將在結束時清空)

例如,如果你有一個self.playerCards作爲UICardView數組要動畫只是把它這樣

NSMutableArray *cardsToBeAnimated = [NSMutableArray arrayWithArray:self.playerCards]; 
[self animateCards:cardsToBeAnimated]; 
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]; 
         } 
    ]; 
}