是否可以在CAKeyFrameAnimation中向后播放路径?

时间:2009-04-03 20:13:30

标签: iphone objective-c core-animation cgpath

我想基于CGPath设置CALayer的位置动画,但我想向后播放它。

有没有办法让路径向后动画,或者反转CGPath?

2 个答案:

答案 0 :(得分:29)

animation.speed = -1;

答案 1 :(得分: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];
}

如果存在更好的方法,我想知道。