I want to add animation to the pie chart.

Asked 2 years ago, Updated 2 years ago, 41 views

I am making a pie chart, but I would like to add an animation that changes the value of the graph from the left to the right.What are the options?

Gray code

CGContextSaveGState(context);

CGFloat x = rect.origin.x;
x+ = rect.size.width/2;

CGFloaty = rect.origin.y;
y+=rect.size.height/2;

CGMutablePathRef path=CGPathCreateMutable();
CGPathAddArc (path, NULL, x, y, x-10, 0, M_PI*2, NO);
CGContextAddPath(context, path);
CGContextSetRGBFillColor (context, 0.6, 0.6, 1.0); // gray

CGContextSetLineWidth (context, 0);
CGContextSetStrokeColorWithColor (context, [UIColor blackColor].CGColor);
CGContextDrawPath(context,kCGPathFillStroke);

CGContextRestoreGState (context);

The pie chart was created by referring to the methods on the following sites.
http://qiita.com/rnsm504/items/1c9e71a318fc8bbd5ab8

Enter a description of the image here Enter a description of the image here

objective-c

2022-09-30 15:31

2 Answers

CALayer as a subclass to accommodate implicit animation.

Implementation example of the graph to be animated

Swift I'm also practicing, so I'm sorry to say that Swift is an example code.It's a mess.

However, Objective-C doesn't make much difference.

Basically, there are four things to do.

How to make it animated

Use CABasicAnimation.

var graph=AnimatableGraph()
graph.frame = CGRect (x:50, y:50, width:200, height:200)
self.view.layer.addSublayer(graph)

variation animation = CABasicAnimation (keyPath: "degre")
// for ten seconds
animation.duration=10
animation.repeatCount=1
// a clockwise turn from nine o'clock
animation.fromValue=0.0
animation.toValue=360.0
// Magic about the state after the animation is complete
animation.removedOnCompletion=false
animation.fillMode=kCAFillModeForwards
graph.addAnimation(animation, forKey: "key")

It's a bit complicated to describe.


2022-09-30 15:31

There are two easy-to-understand methods:

If you are using the method 1, you may want to find out about CADisplayLink.


2022-09-30 15:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.