Identify CAAnimation in an animationDidStop failback call

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
{
    // Prepare the animation from the current position to the new position
    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;
    // Update the layer position so that the layer doesn't snap back when the animation completes.
    layer.position = point;

    // Add the animation, overriding the implicit animation.
    [layer addAnimation:animation forKey:@"position"];
}

and callback:

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

    id animationIDString=[animation valueForKey:@"animationID"];
    // this is coming as an int value sometimes and the code below this fails ..
    if([animationIDString isEqual:@"animation1"]) {
        //animation is 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.

+4

Source: https://habr.com/ru/post/1662218/


All Articles