您可以为动画对象上的键设置任意值。这意味着您可以将您正在制作动画的图层与动画相关联,然后在 -animationDidStop:finished: 中查询它:您可以这样创建动画:
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
// set other fields...
[anim setValue:layerToAnimate forKey:@"layer"];
// Start the animation
[layerToAnimate addAnimation:anim forKey:nil];
然后在动画停止时检查该值:
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
{
CALayer *animatedLayer = [animation valueForKey:@"layer"];
// do something with the layer based on some condition...
// spin off the next animation...
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
[animatedLayer setPosition:position];
[CATransaction commit];
}
这是明确的动画,但它应该给你你需要的东西。