Is it possible to play a path backwards in a CAKeyFrameAnimation?

后端 未结 2 430
梦谈多话
梦谈多话 2021-02-05 09:23

I want to animate the position of a CALayer based on a CGPath, but I want to play it backwards.

Is there any way to animate a path backwards, or reverse a CGPath?

相关标签:
2条回答
  • 2021-02-05 09:46

    This may not be the best solution, but it worked for me. I told the animation to autoreverse, then started it halfway in. But to prevent it from animating back again I needed to terminate it early.

    - (void) animate {
        CAKeyframeAnimation * pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        pathAnimation.path = path;
        pathAnimation.duration = 15.0;
        pathAnimation.calculationMode = kCAAnimationPaced;
        pathAnimation.timeOffset = pathAnimation.duration;
        pathAnimation.autoreverses = YES;
    
        NSString * key = @"pathAnimation";
        [animatedView.layer addAnimation:pathAnimation forKey:key];
        [NSTimer scheduledTimerWithTimeInterval:pathAnimation.duration target:self selector:@selector(endAnimation:) userInfo:key repeats:NO];
    }
    
    - (void) endAnimation:(NSTimer *)timer {
        [animatedView.layer removeAnimationForKey:timer.userInfo];
    }
    

    If a better ways exists, I'd like to know.

    0 讨论(0)
  • 2021-02-05 09:55
    animation.speed = -1;
    

    ?

    0 讨论(0)
提交回复
热议问题