动画没有按预期工作

Animation is not working as expected

我在 xcode 中实现了 corePlot,并且我正在使用饼图。我正在尝试在图表重新加载时创建 3d 翻转动画。这是代码:

CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
scaleAnimation.toValue = [NSNumber numberWithFloat:0.5];
scaleAnimation.duration = 1.0f;
scaleAnimation.removedOnCompletion = NO;
[self.pieChart addAnimation:scaleAnimation forKey:@"scale"];

[self.pieChart reloadData];

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
animation.fromValue = [NSNumber numberWithFloat:0.5];
animation.toValue = [NSNumber numberWithFloat:1.0];
animation.duration = 1.0f;
[self.pieChart addAnimation:animation forKey:@"scale"];

我没有得到理想的效果。我认为正在发生的事情是,两个动画同时发生。 (虽然我不确定这是怎么回事。)

另外,是否可以添加 z 深度?如果可以,怎么做?

更新

我尝试了以下方法:

CABasicAnimation *currentAnition = (CABasicAnimation *)anim;
if (currentAnition == self.scaleAnimation {...}

它没有用。

CAAnimation 符合 KVC,因此我们可以在委托方法中存储您是开始还是结束查找:

{
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    [scaleAnimation setValue:@(YES) forKey:@"scaleAnimation"];
    // set the delegate
    scaleAnimation.delegate = self;
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:0.5];
    scaleAnimation.duration = 1.0f;
    scaleAnimation.removedOnCompletion = NO;
    [self.pieChart addAnimation:scaleAnimation forKey:@"scale"];
    [self.pieChart reloadData];
}

- (void)runSecondAnimation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    [animation setValue:@(YES) forKey:@"secondAnimation"];
    animation.delegate = self;
    animation.fromValue = [NSNumber numberWithFloat:0.5];
    animation.toValue = [NSNumber numberWithFloat:1.0];
    animation.duration = 1.0f;
    [self.pieChart addAnimation:animation forKey:@"scale"];
}

/* Called when the animation either completes its active duration or
 * is removed from the object it is attached to (i.e. the layer). 'flag'
 * is true if the animation reached the end of its active duration
 * without being removed. */

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    BOOL scaleAnimation = [[anim valueForKey:@"scaleAnimation"] boolValue];
    if (scaleAnimation) {
        [self runSecondAnimation];
    }
    BOOL secondAnimation = [[anim valueForKey:@"secondAnimation"] boolValue];
    if (secondAnimation) {
        [self runThirdAnimation];
    }
}

记住你还有:

/* Called when the animation begins its active duration. */
- (void)animationDidStart:(CAAnimation *)anim;