每次 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,这样它就能处理所有的点,但我该怎么做呢?将不胜感激我能得到的任何帮助。
最佳答案
问题在于您正在创建应用于单个 View 元素的多个动画 block ,并且您正在一次应用所有这些动画 block 。
相反,您可以使动画 block 在完成时触发另一个动画。
Apple View Programming Documentation中有一个例子:“ list 4-2 显示了一个动画 block 的示例,该动画 block 使用完成处理程序在第一个动画完成后启动新动画”
我在这里发布了示例,以便您可以感受一下,但是请阅读文档!:
- (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.
}
}
关于objective-c - 如何像这样在for循环中调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12311488/