理想は、次のキーフレームを実行する前に 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 はすべてのステップでインクリメントされます。各アニメーションが停止したら、アニメーション中のスプライト イメージの下にスプライト イメージを描画し、移動するポイントがなくなるまでアニメーションを再開します。そして、終了します。
お役に立てれば!