iOS appeased diagram

Source: Internet
Author: User

iOS appeased Figure 1. The following

2. appeased diagram implementation steps are as follows:
    • 1. Drag a UIView control into the Main.storyboard and set its custom Class to Hmpieview. Such as

    • 2. New Hmpieview class

////  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.