2014-04-15 24 views
1

我正在CS193P上工作,我想创建一个效果,其中卡片从0,0一个接一个地捕捉到位。我试图链接动画,但一起飞行的意见也是我试图使用UIDynamicAnimator和同样的事情发生。所有的观点都在一起。这是我必须捕捉视图的代码。是否有可能使用UISnapBehavior连续捕捉UIViews

-(void)snapCardsForNewGame 
{ 
    for (PlayingCardView *cardView in self.cards){ 
     NSUInteger cardViewIndex = [self.cards indexOfObject:cardView]; 
     int cardColumn = (int) cardViewIndex/self.gameCardsGrid.rowCount; 
     int cardRow = (int) cardViewIndex % self.gameCardsGrid.rowCount; 
     UISnapBehavior *snapCard = [[UISnapBehavior alloc]initWithItem:cardView snapToPoint:[self.gameCardsGrid centerOfCellAtRow:cardRow inColumn:cardColumn]]; 
     snapCard.damping = 1.0; 
     [self.animator addBehavior:snapCard]; 

    } 


} 


-(void)newGame 
{ 
    NSUInteger numberOfCardsInPlay = [self.game numberOfCardsInPlay]; 
    for (int i=0; i<numberOfCardsInPlay; i++) { 
     PlayingCardView *playingCard = [[PlayingCardView alloc]initWithFrame:CGRectMake(0, 0, 50, 75)]; 
     playingCard.faceUp = YES; 
     [playingCard addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(flipCard:)]]; 
     [self.cards addObject:playingCard]; 
     //NSUInteger cardViewIndex = [self.cards indexOfObject:playingCard]; 
     //int cardColumn = (int) cardViewIndex/self.gameCardsGrid.rowCount; 
     //int cardRow = (int) cardViewIndex % self.gameCardsGrid.rowCount; 

     // playingCard.frame = [self.gameCardsGrid frameOfCellAtRow:cardRow inColumn:cardColumn]; 
     playingCard.center = CGPointMake(0, 0); 
     [self.gameView addSubview:playingCard]; 
     [self snapCardsForNewGame]; 
    } 
} 

在这种情况下使用它有意义吗?我尝试了几件不同的事情来让卡片一个接一个地飞,但无法完成。

提前致谢!

+0

我以前没用过这个,但是UIDynamicAnimator有一个你可以自己设置的代理。当动态设置达到平衡时,动画设计师(我认为)会暂停并告诉代理它已暂停。所以你不会在这里写一个循环。你拍一张牌,等待暂停,再拍一张牌...... – danh

回答

3

由于您在同一时间添加了所有UISnapBehaviors,动画制作者将它们一起运行。延迟添加到动画制作者,他们将自己动画。

-(void)snapCardsForNewGame 
{ 
    for (PlayingCardView *cardView in self.cards){ 
     NSUInteger cardViewIndex = [self.cards indexOfObject:cardView]; 
     int cardColumn = (int) cardViewIndex/self.gameCardsGrid.rowCount; 
     int cardRow = (int) cardViewIndex % self.gameCardsGrid.rowCount; 
     UISnapBehavior *snapCard = [[UISnapBehavior alloc]initWithItem:cardView snapToPoint:[self.gameCardsGrid centerOfCellAtRow:cardRow inColumn:cardColumn]]; 
     snapCard.damping = 1.0; 

     NSTimeInterval delayTime = 0.01 * cardViewIndex; 
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
      [self.animator addBehavior:snapCard]; 
     }); 
    } 
} 
+0

完美工作。我只是将延迟时间更改为0.05,以使卡片出来速度稍慢。谢谢! – Yan

+0

太棒了!快乐编码:) –

相关问题