动画未按预期工作

时间:2015-05-05 23:30:05

标签: ios objective-c core-plot cabasicanimation

我在corePlot中实施了xcode,并且我使用了饼图。我正在尝试在图表重新加载时创建一个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 {...}

它没有用。

1 个答案:

答案 0 :(得分:0)

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;