Reputation: 4410
I need to do something after animation stops so I put self as delegate
CAShapeLayer* myLayer = [CAShapeLayer layer];
...
CABasicAnimation * animation;
...
animation.delegate=self;
...
[myLayer addAnimation:animation];
This is just a simplified example to explain the situation. As usual this is the called method in the end
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
I need another parameter and I would not like to put it as a member of the class because it will be seen by the other methods. I would need to put as delegate another method of mine with an integer parameter added at the creation of the animation, in order to have it as a local parameter. Example:
-(void)myAnimationDidStop:(CAAnimation *)anim finished:(BOOL)flag index:(int) ind
Is there a way to reach this goal?
Upvotes: 0
Views: 228
Reputation: 56625
You can actually set values for any key on both layers and animation objects. Note that since the animation gets copied when it's added to the layer, you have to set the value before adding it to the layer, otherwise you are modifying a different object then the one that will eventually finish.
This behavior is documented in the Core Animation Programming Guide:
The CAAnimation and CALayer classes are key-value coding compliant container classes, which means that you can set values for arbitrary keys. Even if the key someKey is not a declared property of the CALayer class, you can still set a value for it as follows:
// before adding the animation (because the animation get's copied)
[theAnimation setValue:yourValueHere forKey:@"yourKeyHere"];
and then retrieve it in animationDidStop:
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
id yourExtraData = [anim valueForKey:@"yourKeyHere"];
if (yourExtraData) {
// do something with it
}
}
Upvotes: 1
Reputation: 1483
Your delegate could keep an administration of the data necessary for postprocessing per animation. I'm thinking of an NSMutableDictionary with the animation object as key:
// where you setup the animation
animation.delegate=self;
MyAnimationDataClass *myAnimationData;
[self.runningAnimations setObject: myAnimationData forKey: animation];
Then in your delegate method callback:
-(void)myAnimationDidStop:(CAAnimation *)anim finished:(BOOL)flag index:(int) ind {
MyAnimationDataClass *myData = [self.runningAnimations objectForKey: anim];
if (myData) {
// do your postprocessing
}
}
Upvotes: 2