我知道您必须使用此方法来获得动画完成时的委托(delegate)方法:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {

问题是,如何区分两个或两个以上的CAAnimations?

我用谷歌搜索,但没有发现任何有用的东西。

请与我分享您如何做到这一点!

谢谢!

最佳答案

您可以为CAAnimation实例设置键/值对象,如下所示:

CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[theAnimation setValue:@"animation1" forKey:@"id"];
theAnimation.delegate = self;

CABasicAnimation *theAnimation2 = [CABasicAnimation animationWithKeyPath:@"opacity"];
[theAnimation2 setValue:@"animation2" forKey:@"id"];
theAnimation2.delegate = self;

检查在委托(delegate)方法中调用了哪一个:
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if([[anim valueForKey:@"id"] isEqual:@"animation1"]) {
        NSLog(@"animation1");
    }
    if([[anim valueForKey:@"id"] isEqual:@"animation2"]) {
        NSLog(@"animation2");
    }
}

08-24 13:14