BeachRunnerFred
BeachRunnerFred

Reputation: 18558

Nesting an unknown number of UIView animations

I'm building an iPhone card game and I want to animate the players cards off the table at the end of each round. The player can have any number of cards at the end of each round, so I can't nest the animation code in any kind of static way. If I have the following code to animate two card view objects off the table...

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

...how can I structure my code to animate an unknown number of card view objects that are in an NSOrderedList?

Thanks so much for your wisdom!

Upvotes: 0

Views: 143

Answers (2)

Moxy
Moxy

Reputation: 4212

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

You can call animateCards with an array of UICardView. (make sure to make a copy because the array will be empty at the end)

if for example you have a self.playerCards as an array of UICardView you want to animate just call it this way

NSMutableArray *cardsToBeAnimated = [NSMutableArray arrayWithArray:self.playerCards];
[self animateCards:cardsToBeAnimated];

Upvotes: 1

user529758
user529758

Reputation:

...or recursion, perhaps:

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

Upvotes: 0

Related Questions