如何像这样调用for循环中的方法

时间:2012-09-07 03:53:16

标签: objective-c ios xcode

每次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,直到它发出第5个对象索引而不是单独调用所有索引号。例如,它不是在所有点处交易卡而是仅处理(200,100) )。我需要一些方法来每次调用animaitedealingwithplayer所以它将处理所有的点,但我该怎么做?我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:1)

问题在于您正在创建适用于单个视图元素的多个动画块,并且您正在同时应用这些动画块。

相反,您可以使动画块在完成时触发另一个动画。

Apple View Programming Documentation中有一个示例:&#34; 清单4-2显示了一个动画块的示例,它使用完成处理程序在第一个完成后启动新动画&#34;

我在这里发布了这个例子,所以你可以感受到它,但请阅读文档!:

- (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.
   }
}