Multiple CAAnimations for animationDidStop method?

自作多情 提交于 2019-12-12 07:09:43

问题


I know you have to use this method to get the delegate method for when the animation has finished:

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

The problem is, how would I distinguish between multiple CAAnimations like 2 or more?

I googled this and I haven't found anything useful.

Please share with me on how you accomplished this!

Thanks!


回答1:


You can set key/value objects for CAAnimation instance like this:

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;

Check which one was called in delegate method:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if([[anim valueForKey:@"id"] isEqual:@"animation1"]) {
        NSLog(@"animation1");
    }
    if([[anim valueForKey:@"id"] isEqual:@"animation2"]) {
        NSLog(@"animation2");
    }
}



回答2:


A CAAnimation object is supposed to be reused from time to time and that's why I don't like to give it a certain key (since it's not unique). What makes it unique is the association with a CALayer with addAnimation:forKey:. For this reason I use the following code in animationDidStop:

if animation == layer.animationForKey(AnimationKeys.scaleUp) {
   // scaleUp animation has completed
}


来源:https://stackoverflow.com/questions/7748722/multiple-caanimations-for-animationdidstop-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!