L'idéal serait que CAKeyframeAnimation notifie son délégué avant de faire l'image-clé suivante; Puisque je ne pense pas que ce soit possible (?), la seule façon de penser à faire quelque chose comme ça est d'utiliser un tableau de positions, et d'utiliser des instances consécutives de CABasicAnimation pour le faire. C'est comme un "pauvre" CAKeyframeAnimation.
Quelque chose comme ceci:
- (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];
}
Dans ce cas, les animations exécuter un après l'autre, avec des valeurs spécifiées dans le _positions NSArray Ivar et le _step est incrémenté à chaque étape. Lorsque chaque animation s'arrête, nous dessinons une image de sprite en-dessous de celle que nous animons, et nous redémarrons notre animation, jusqu'à ce qu'il n'y ait plus de points à atteindre. Et puis nous finissons.
Espérons que cela aide!