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
objective-c
CALayer
as a subclass to accommodate implicit animation.
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.
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.
There are two easy-to-understand methods:
If you are using the method 1, you may want to find out about CADisplayLink.
© 2024 OneMinuteCode. All rights reserved.