iOS appeased Figure 1. The following
2. appeased diagram implementation steps are as follows:
//// HMPieView.h// 03-饼图//#import <UIKit/UIKit.h>@interface HMPieView : UIView@end
////HMPIEVIEW.M//03-Pie chart//#import "HMPieView.h" #import "uicolor+random.h" @implementation hmpieview - (ID) initWithFrame: (CGRect) frame{ Self= [SuperInitwithframe:frame];if( Self) {//initialization code}return Self;}//Only override Drawrect:if perform custom drawing.//An empty implementation adversely affects performance during animation.- (void) DrawRect: (CGRect) rect{//Drawing code Nsarray*data = @[@ -,@ -,@ -];//1. Get ContextCgcontextref CTX = Uigraphicsgetcurrentcontext ();//2. Stitching path CgpointCenter = Cgpointmake ( the, the);CGFloatRadius = -;CGFloatStarta =0;CGFloatAngle =0;CGFloatENDA =0; for(NSNumber*number in data) {//2. Stitching pathStarta = ENDA; Angle = number. Intvalue/100.0* M_PI *2; ENDA = Starta + angle; Uibezierpath *path = [Uibezierpath bezierpathwitharccenter:center radius:radius Startangle:starta EndAngle:endA Clockwise:YES]; [Path Addlinetopoint:center]; [[UicolorRandomcolor] set];//Add a path to the contextCgcontextaddpath (CTX, Path. Cgpath);//RenderingCgcontextfillpath (CTX); }}- (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{CGFloatA = Arc4random_uniform (6);//cgfloat a = Arc4random ()%6; NSLog(@"Random number--%f", a); [ SelfSetneedsdisplay];} - (void) drawpie{//1. Get ContextCgcontextref CTX = Uigraphicsgetcurrentcontext ();//2. Stitching path CgpointCenter = Cgpointmake ( the, the);CGFloatRadius = -;CGFloatStarta =0;CGFloatAngle =0;CGFloatENDA =0;//First sectorAngle = -/100.0* M_PI *2; ENDA = Starta + angle; Uibezierpath *path = [Uibezierpath bezierpathwitharccenter:center radius:radius Startangle:starta EndAngle:endA Clockwise:YES]; [Path Addlinetopoint:center];//Add to ContextCgcontextaddpath (CTX, Path. Cgpath); [[UicolorRedcolor] set];//RenderingCgcontextfillpath (CTX);//A second sectorStarta = ENDA; Angle = -/100.0* M_PI *2; ENDA = Starta + angle; Uibezierpath *path1 = [Uibezierpath bezierpathwitharccenter:center radius:radius Startangle:starta EndAngle:endA Clockwise:YES]; [Path1 Addlinetopoint:center];//Add to ContextCgcontextaddpath (CTX, path1. Cgpath); [[UicolorGreencolor] set];//RenderingCgcontextfillpath (CTX);//A third sectorStarta = ENDA; Angle = -/100.0* M_PI *2; ENDA = Starta + angle; Uibezierpath *path2 = [Uibezierpath bezierpathwitharccenter:center radius:radius Startangle:starta EndAngle:endA Clockwise:YES]; [Path2 Addlinetopoint:center];//Add to ContextCgcontextaddpath (CTX, path2. Cgpath); [[UicolorBluecolor] set];//RenderingCgcontextfillpath (CTX); }@end
- 3. New Uicolor+random Class
//// UIColor+Random.h// 03-饼图//#import <UIKit/UIKit.h>@interface UIColor (Random)+ (UIColor *)randomColor;@end
////UICOLOR+RANDOM.M//03-Pie chart//#import "uicolor+random.h" @implementation uicolor (Random)+ (Uicolor*) randomcolor{/ * Color has two representations of RGB RGBA RGB 8 r,g,b per color Channel 8 binary 255 r,g,b Each color value 0 ~255 120/255.0 */ CGFloatR = Arc4random_uniform ( the) /255.0;CGFloatg = Arc4random_uniform ( the) /255.0;CGFloatb = Arc4random_uniform ( the) /255.0;return[UicolorColorwithred:r green:g blue:b Alpha:1];}@end
iOS appeased diagram