views:

1624

answers:

1

Hi,

I've been looking into core-plot for the iPhone and I'm having trouble finding any examples of animation actually being used.

What I need to see is an example of how to use core-plots animation to add an extra plot to a graph when someone clicks a button.

If anyone can produce and example, or show me a link to one, that would be great.

Regards, Craig

+4  A: 

The official CPAnimation classes within Core Plot are just stubs right now. At some point, we'll enable the full functionality of those.

In the meantime, every visible element in Core Plot is a Core Animation CALayer, so you can animate these using existing Core Animation methods. For example, if you have a plot called dataSourceLinePlot (like in the test Core Plot iPhone application), you could start the plot off with an opacity of 0.0:

dataSourceLinePlot.opacity = 0.0f;
[graph addPlot:dataSourceLinePlot];

and then animate its opacity to fade it in:

CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.duration = 1.0f;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
[dataSourceLinePlot addAnimation:fadeInAnimation forKey:@"animateOpacity"];

This will fade in a new plot on an existing graph over a one second interval. You could also do something similar to animate it in from a side or use a transform to scale it up into position. CATransitions could also be used to achieve these kind of effects.

EDIT (1/17/2010): The Core Plot iPhone test application now contains an example of the fade-in animation described above.

Brad Larson
Brad, I have seen how you're solution works in the example program on google code.In my problem I want the animation to start after a button click, this doesn't work by copying the animation code into the button click event. Any help?
Craig Warren
It doesn't matter where the animation is placed, it should work the same way. Are you sure the button click is triggering your method? In the sample code, a reference to the dataSourceLinePlot is not maintained outside of the -viewDidLoad method. Make sure that the reference to this plot is correct within your method, so that the animation is not being added to the wrong layer, or to nil.
Brad Larson
Sorry Brad, I had wired the button action to the wrong event (drag outside)... problems solved.Loving Core-Plot, would love to see more features.
Craig Warren