I had a problem where I had a series of overlapping CATransition / CAAnimation sequences, all of which I needed to perform custom operations when the animations stopped, but I only wanted one delegate handler for animationDidStop.
However, I had a problem, there didn't appear to be a way to uniquely identify each CATransition / CAAnimation in the animationDidStop delegate.
I solved this problem via the key / value system exposed as part of CAAnimation.
When you start your animation use the setValue method on the CATransition / CAAnimation to set your identifiers and values to use when animationDidStop fires:
-(void)volumeControlFadeToOrange
{
CATransition* volumeControlAnimation = [CATransition animation];
[volumeControlAnimation setType:kCATransitionFade];
[volumeControlAnimation setSubtype:kCATransitionFromTop];
[volumeControlAnimation setDelegate:self];
[volumeControlLevel setBackgroundImage:[UIImage imageNamed:@"SpecialVolume1.png"] forState:UIControlStateNormal];
volumeControlLevel.enabled = true;
[volumeControlAnimation setDuration:0.7];
[volumeControlAnimation setValue:@"Special1" forKey:@"MyAnimationType"];
[[volumeControlLevel layer] addAnimation:volumeControlAnimation forKey:nil];
}
- (void)throbUp
{
doThrobUp = true;
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionFade];
[animation setSubtype:kCATransitionFromTop];
[animation setDelegate:self];
[hearingAidHalo setBackgroundImage:[UIImage imageNamed:@"m13_grayglow.png"] forState:UIControlStateNormal];
[animation setDuration:2.0];
[animation setValue:@"Throb" forKey:@"MyAnimationType"];
[[hearingAidHalo layer] addAnimation:animation forKey:nil];
}
In your animationDidStop delegate:
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{
NSString* value = [theAnimation valueForKey:@"MyAnimationType"];
if ([value isEqualToString:@"Throb"])
{
//... Your code here ...
return;
}
if ([value isEqualToString:@"Special1"])
{
//... Your code here ...
return;
}
//Add any future keyed animation operations when the animations are stopped.
}
The other aspect of this is that it allows you to keep state in the key value pairing system instead of having to store it in your delegate class. The less code, the better.
Be sure to check out the Apple Reference on Key Value Pair Coding.
Are there better techniques for CAAnimation / CATransition identification in the animationDidStop delegate?
Thanks, --Batgar
Batgar's technique is too complicated. Why not take advantage of the forKey parameter in addAnimation? It was intended for this very purpose. Just take out the call to setValue and move the key string to the addAnimation call. For example:
[[hearingAidHalo layer] addAnimation:animation forKey:@"Throb"];
Then, in your animationDidStop callback, you can do something like:
if (theAnimation == [[hearingAidHalo layer] animationForKey:@"Throb"]) ...
I just came up with an even better way to do completion code for CAAnimations:
I created a typedef for a block:
typedef void (^animationCompletionBlock)(void);
And a key that I use to add a block to an animation:
#define kAnimationCompletionBlock @"animationCompletionBlock"
Then, if I want to run animation completion code after a CAAnimation finishes, I set myself as the delegate of the animation, and add a block of code to the animation using setValue:forKey:
animationCompletionBlock theBlock = ^void(void)
{
//Code to execute after the animation completes goes here
};
[theAnimation setValue: theBlock forKey: kAnimationCompletionBlock];
Then, I implement an animationDidStop:finished: method, that checks for a block at the specified key and executes it if found:
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
animationCompletionBlock theBlock = [theAnimation valueForKey: kAnimationCompletionBlock];
if (theBlock)
theBlock();
}
The beauty of this approach is that you can write the cleanup code in the same place where you create the animation object. Better still, since the code is a block, it has access to local variables in the enclosing scope in which it's defined. You don't have to mess with setting up userInfo dictionaries or other such nonsense, and don't have to write an ever-growing animationDidStop:finished: method that gets more and more complex as you add different kinds of animations.
Truth be told, CAAnimation should have a completion block property built into it, and system support for calling it automatically if one is specified. However, the above code gives you that same functionality with only a few lines of extra code.
All other answers are way too complicated! Why don't you just add your own key to identify the animation?
This solution is very easy all you need is to add your own key to the animation (animationID in this example)
Insert this line to identify animation1:
[myAnimation1 setValue:@"animation1" forKey:@"animationID"];
and this to identify animation2:
[myAnimation2 setValue:@"animation2" forKey:@"animationID"];
Test it like this:
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
{
if([[animation valueForKey:@"animationID"] isEqual:@"animation1"]) {
//animation is animation1
} else if([[animation valueForKey:@"animationID"] isEqual:@"animation2"]) {
//animation is animation2
} else {
//something else
}
}
It does not require any instance variables:
The second approach will only work if you explicitly set your animation to not be removed on completion before running it:
CAAnimation *anim = ...
anim.removedOnCompletion = NO;
If you fail to do so, your animation will get removed before when it completes, and the callback will not find it in the dictionary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With