How to determine CAAnimation after completion? I tried KVO by setting the string value as animIDID and checking it in the animationDidStop method. But KVO sometimes returns integerinstead of an animation identifier string, and the application crashes.
Here is my code:
-(void)moveLayer:(CALayer*)layer to:(CGPoint)point duration:(NSTimeInterval)duration animationID:(NSString*)animationid
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [layer valueForKey:@"position"];
[animation setDuration:duration];
animation.toValue = [NSValue valueWithCGPoint:point];
animation.delegate=self;
[animation setValue:animationid forKey:@"animationID"];
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
layer.position = point;
[layer addAnimation:animation forKey:@"position"];
}
and callback:
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
{
if (flag) {
id animationIDString=[animation valueForKey:@"animationID"];
if([animationIDString isEqual:@"animation1"]) {
_movieBalloonImageView.hidden=NO;
_storiesBalloonImageView.hidden=NO;
_rhymesBalloonImageView.hidden=NO;
[self.navigationController popViewControllerAnimated:NO];
}
}
}
What could be the possible reason? Is it because the animation is destroyed before calling this delegate? I set removedOnCompletionto NO and fillmodeto kCAFillModeForwards. But the code does not work as expected.
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
Or is there an alternative method for this? I call the method moveLayerin several places in the viewController.