理想的情况是 CAKeyframeAnimation 在执行以下关键帧之前通知其委托;因为我认为这是不可能的(?)我能想到做这样的事情的唯一方法是使用一个位置数组,并使用连续的 CABasicAnimation 实例来做到这一点。这就像一个“穷人的”CAKeyframeAnimation。
像这样的东西:
- (void)viewDidLoad
{
[super viewDidLoad];
_step = 0;
_positions = [[NSArray alloc] initWithObjects:[NSValue valueWithCGPoint:CGPointMake(20.0, 20.0)],
[NSValue valueWithCGPoint:CGPointMake(40.0, 80.0)],
[NSValue valueWithCGPoint:CGPointMake(60.0, 120.0)],
[NSValue valueWithCGPoint:CGPointMake(80.0, 160.0)],
[NSValue valueWithCGPoint:CGPointMake(100.0, 200.0)],
[NSValue valueWithCGPoint:CGPointMake(120.0, 240.0)],
[NSValue valueWithCGPoint:CGPointMake(140.0, 280.0)],
[NSValue valueWithCGPoint:CGPointMake(160.0, 320.0)],
[NSValue valueWithCGPoint:CGPointMake(180.0, 360.0)],
[NSValue valueWithCGPoint:CGPointMake(200.0, 400.0)],
nil];
[self moveToNextPosition];
}
- (void)moveToNextPosition
{
if (_step < [_positions count] - 1)
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [_positions objectAtIndex:_step];
animation.toValue = [_positions objectAtIndex:(_step + 1)];
animation.delegate = self;
animation.removedOnCompletion = YES;
[_sprite.layer addAnimation:animation forKey:@"position"];
++_step;
}
else
{
_sprite.center = [[_positions objectAtIndex:_step] CGPointValue];
}
}
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished
{
UIImageView *trail = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sprite.png"]];
trail.center = [[_positions objectAtIndex:_step] CGPointValue];
[self.view insertSubview:trail belowSubview:_sprite];
[trail release];
[self moveToNextPosition];
}
在这种情况下,动画一个接一个地执行,其值在 _positions NSArray ivar 中指定,并且 _step 在每一步都递增。当每个动画停止时,我们在我们正在制作动画的那个下方绘制一个精灵图像,然后我们重新开始我们的动画,直到没有更多的点可以移动。然后我们完成。
希望这可以帮助!