开发者

Animate multiple UIView in a cycle

i'm creating a card game on iphone.

My problem is that i want to animate the cards at the beginning of the game making the cards animate from a point to another point in a deck.

I move my cards that are UIView, in a for cycle. this is what i do

With this code, all the cards move together, i need to move the cards separately one after ano开发者_如何学编程ther

    CGPoint point;

// Create the deck of playing cards
for (int i = 0; i < 28; i++) {  
    CardView *aCardView = [self.mazzo objectAtIndex:i];

    point.x = -100;
    point.y = 200;
    aCardView.center = point;   

    aCardView.zPosition = i;

    [self.viewGioco addSubview:aCardView];

    [aCardView release];

            //Here i call the method to position the card
    [aCardView positionCard];

}

in the card view there are this methods

-(void)positionCard{
    [self performSelector:@selector(_positionCard) withObject:nil afterDelay:0.0];
}
-(void)_positionCard{

[UIView beginAnimations:@"posizionacarta" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.3f];

CGPoint point;

point.x = 280 + ((arc4random() % 2) - 1);
point.y = 240 + ((arc4random() % 2) - 1);

self.center = point;

[UIView commitAnimations];

[self setNeedsLayout];  

}


I think the easiest thing would be to use the counter i to calculate a slight delay based on the index of the card - and then passing this to the the positionCard method.

So:

[aCardView positionCardWithDelay:i/10];

And:

-(void)positionCardWithDelay:(NSNumber)delay
{
    // Call _position card after a slight delay
    [self performSelector:@selector(_positionCard) withObject:nil afterDelay:delay];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜