Paused / restarted animation does not invoke delegation methods

I have a CAKeyframeAnimation that calls animationDidStart: and animationDidStop: completed: as expected when the animation is allowed to run its course.

When I pause animation during interrupts (calls, home btn pressed, etc.) animationDidStop: finished: fires with the NO flag, as expected.

However, after restarting the paused animation, the animation DidStart: and animationDidStop: completed: no longer starts. Animation visually continues and ends, but I do not receive the event. I need to know when the animation stops, so I can restart the game loop.

I followed this to pause / restart the animation.

Please tell me what I'm doing wrong. Thank!

- (void)takeFlight
{   
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.duration = FLY_ANIM_TIME;
    pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    pathAnimation.delegate = self;

    CGPoint endPoint = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/3);
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPathMoveToPoint(curvedPath, NULL, xPos, yPos);
    CGPathAddQuadCurveToPoint(curvedPath, NULL, -100.0, -50.0, 200.0, 100.0);
    pathAnimation.path = curvedPath;
    CGPathRelease(curvedPath);

    [shipView.layer addAnimation:pathAnimation forKey:@"flyin"];
    shipView.center = endPoint; 
}

-(void)pauseFlight
{
    CALayer *layer = (CALayer *)shipView.layer;
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

-(void)resumeFlight
{   
    CALayer *layer = (CALayer *)shipView.layer;
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}

- (void)animationDidStart:(CAAnimation *)animation
{
    NSLog(@"anim did start");
}

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
{   
    NSLog(@"anim did stop, flag is: %d", flag);

}
+3

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


All Articles