在CAKeyFrameAnimation中,是否有可能倒放一个路径?

18

我希望能够基于CGPath动画CALayer的位置,但我想要反向播放。

是否有任何方法可以反向动画路径,或反转CGPath?

2个回答

32
animation.speed = -1;

?


是的,这绝对更好。我希望我早点想到这个。 - Cory Kilger
你是在哪里学到的?我在苹果文档中找不到这个功能。 - surfrider

4

这可能不是最好的解决方案,但对我有效。我告诉动画自动反转,然后从一半开始它。但为了防止它再次动画化,我需要提前终止它。

- (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];
}

如果有更好的方法存在,我想了解一下。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接