Use CAShapeLayer to implement a simple pie chart (PieView), cashapelayer
I wrote a simple PieView, demo here: pie and https://github.com/xyfeng/XYPieChart code implementation method: to draw a pie chart, the value of only the corresponding values of each sector and the corresponding color, however, there may be many additional elements that need to be displayed (such as the font color and font size). Therefore, the data required for each sector is encapsulated into a model object for future extension. 1. To draw each slice, you need to know the startAngle and endAngle of the slice. Therefore, you need to calculate the percentage and angle of each slice, and then the start of the first slice is 0, the end is the angle of the first slice, the start of the second slice is the end of the first slice, and the end of the second slice is the sum of the angle of the first two slice slices, similarly, the pie chart is drawn from the position of-0.5 * Pi, so the startAngle and endAngle of each slice are calculated and saved to the array respectively. 2. Draw each slice and use UIBezierPath's-(void) addArcWithCenter :( CGPoint) center radius :( CGFloat) radius startAngle :( CGFloat) startAngle endAngle :( CGFloat) endAngle clockwise :( BOOL) clockwise NS_AVAILABLE_IOS (4_0); Method Note: here we use the moveToPoint and addLineToPoint methods to draw a closed curve for later judgment. Add it to self. layer in sequence. 3. add the description label. Here, the center of each label is calculated, and then add the label directly to the view. The center position is calculated based on the point in the circle using the ry method (forgetting to fill the junior high school mathematics. 4, rotate the animation here or use the mask attribute of the layer to achieve partial rendering effect, refer to my previous explanation: http://www.cnblogs.com/Phelthas/p/4643400.html 5, add click event here only use a tapGesture to add to view, then, the location of gesture is used to determine the region of the vertex. How can we determine that the location is not in a certain area ??? Apple provides A tall method:/* Return true if 'point' is contained in 'path'; false otherwise. A point
Is contained in a path if it is inside the painted region when the path
Is filled; if 'eofill' is true, then the even-odd fill rule is used
Evaluate the painted region of the path, otherwise, the winding-number
Fill rule is used. If 'M' is non-NULL, then the point is transformed
'M' before determining whether the path contains it .*/
CG_EXTERN bool CGPathContainsPoint (CGPathRef _ nullable path,
Const CGAffineTransform * _ nullable m, CGPoint point, bool eoFill) CG_AVAILABLE_STARTING (_ MAC_10_4, _ IPHONE_2_0); example: CGPoint location = [sender locationInView: sender. view]; CGAffineTransform transform = CGAffineTransformIdentity; NSInteger index =-1; for (int I = 0; I <self. subLayerArray. count; I ++ ){
CAShapeLayer * shapeLayer = self. subLayerArray [I];
If (CGPathContainsPoint (shapeLayer. path, & transform, location, 0 )){
Index = I;
Break;
} 6. Use the delegate method to callback the click event. This is encapsulation for external calls and logical separation. The code is neat and tidy ~~