Search code examples
iosobjective-canimationcore-plotpie-chart

Why is this not animating?


I implemented corePlot in my xcode project. I have a pie chart that I'm trying to animate. Here is my code:

- (void)configureChart
{
    CPTGraph *graph = self.hostView.hostedGraph;

    CPTPieChart *pieChart = [[CPTPieChart alloc] init];
    pieChart.dataSource = self;
    pieChart.delegate = self;
    pieChart.pieRadius = (self.hostView.bounds.size.height * 0.7) / 2;
    pieChart.startAngle = M_PI_4;
    pieChart.sliceDirection = CPTPieDirectionClockwise;

    [graph addPlot:pieChart];

This is what I tried:

    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotation.removedOnCompletion = YES;
    rotation.fromValue = [NSNumber numberWithFloat:0.0f];
    rotation.duration = 1.0f;
    rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    rotation.delegate = self;
    [pieChart addAnimation:rotation forKey:@"rotation"];
}

When I run the app, it doesn't animate. What am I doing wrong, and how can I fix it?

Update 1

I tried the following:

[CPTAnimation animate:pieChart property:@"startAngle" from:pieChart.startAngle to:pieChart.endAngle duration:1.0];

That didn't have the desired effect. The chart would show for a second, then disappear.

Update 2

I'm trying to get this effect: http://jsfiddle.net/ozgr1wfx/

I'm not sure what I'm doing wrong.


Solution

  • I finally got it thanks to @RoryMcKinnel.

    [CPTAnimation animate:pieChart property:@"endAngle" from:pieChart.startAngle + M_PI * 2 to:pieChart.startAngle  duration:2.0];
    

    This will animate your pie chart like this: http://jsfiddle.net/ozgr1wfx/.

    Again. Thanks to @RoryMcKinnel who put me on the right track to get it!