2012-09-07 28 views
0

我需要在每次執行for循環時調用函數動畫處理函數。如何在for循環中調用方法如此

-(void)animateDealingToPlayer:(Player *)player withDelay:(NSTimeInterval)delay 
{ 
self.frame = CGRectMake(-100.0f, -100.0f, CardWidth, CardHeight); 
self.transform = CGAffineTransformMakeRotation(M_PI); 


NSArray *position = [NSArray arrayWithObjects: 

        [NSValue valueWithCGPoint:CGPointMake(100, 100)],        
        [NSValue valueWithCGPoint:CGPointMake(0, 0)],            
        [NSValue valueWithCGPoint:CGPointMake(0, 0)],       
        [NSValue valueWithCGPoint:CGPointMake(200, 100)],       
        [NSValue valueWithCGPoint:CGPointMake(200, 100)],             


        nil];    


for(int i=0; i<6; i++) { 
    NSValue *value = [position objectAtIndex:i]; 
    CGPoint point = [value CGPointValue]; 
    NSLog(@"%@",NSStringFromCGPoint(point)); 

    _angle = [self angleForPlayer:player]; 

    [UIView animateWithDuration:0.2f 
          delay:delay 
         options:UIViewAnimationOptionCurveEaseOut 
        animations:^ 
    { 





     self.center = point;    



     self.transform = CGAffineTransformMakeRotation(_angle); 
    } 
        completion:nil]; 

現在它正在改寫self.center一遍又一遍,直到它給出了第五對象的索引,而不是調用所有的索引號的individually.For例子,而不是處理卡在所有點的它處理的僅在(200,100)。我需要一些方法來每次調用animaitedealingwithplayer,所以它會處理所有的點,但我該怎麼做?將感謝我能得到的任何幫助。

+0

你爲什麼要設置中心,並在「self」而不是某個玩家對象上進行轉換? –

+0

我不會發布整個代碼,但它需要是這樣的。由於有多個玩家。 –

+0

嘗試更改元素5的位置 - 現在是否生成動畫?我認爲問題在於你正在動畫一個元素(被處理的'卡')5次 - 我不知道你是否被允許疊加這樣的動畫;最後一個是唯一使用的,或者所有動畫同時適用於該卡。 –

回答

1

問題是您正在創建適用於單個視圖元素的多個動畫塊,並且您一次都應用這些動畫塊。

相反,您可以使動畫塊在完成時觸發另一個動畫。

有一個在Apple View Programming Documentation一個例子:「清單4-2顯示了使用完成處理的第一個完成後啓動一個新的動畫的動畫塊的例子」

我張貼這裏的例子,所以你可以去感受它,但去閱讀文檔!:

- (IBAction)showHideView:(id)sender 
{ 
    // Fade out the view right away 
    [UIView animateWithDuration:1.0 
        delay: 0.0 
        options: UIViewAnimationOptionCurveEaseIn 
        animations:^{ 
             thirdView.alpha = 0.0; 
        } 
        completion:^(BOOL finished){ 
            // Wait one second and then fade in the view 
            [UIView animateWithDuration:1.0 
                 delay: 1.0 
                 options:UIViewAnimationOptionCurveEaseOut 
                 animations:^{ 
                    thirdView.alpha = 1.0; 
                 } 
                 completion:nil]; 
        }]; 
} 

在你的榜樣,你應該(在僞代碼):

// center card on dealer 
Animate: ^{ 
    // move card to player1 
}, completion: ^{ 
    // center card on dealer 
    Animate: ^{ 
    // move card to player2 
    }, completion: ^{ 
    //center 
    // animate again, and again etc. 
    } 
}