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

太棒了!快樂編碼:) –

相關問題