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

久未见 提交于 2019-12-03 11:29:45

问题


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?


回答1:


animation.speed = -1;

?




回答2:


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.



来源:https://stackoverflow.com/questions/715546/is-it-possible-to-play-a-path-backwards-in-a-cakeyframeanimation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!