0

我正在构建一个 iPhone 纸牌游戏,我想在每轮结束时为玩家卡片制作动画。玩家可以在每一轮结束时拥有任意数量的牌,所以我不能以任何静态方式嵌套动画代码。如果我有以下代码来为桌子上的两个卡片视图对象设置动画......

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。(确保复制,因为数组最后会是空的)

例如,如果你有一个 self.playerCards 作为 UICardView 的数组,你想要动画就这样调用它

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 回答