UIView:
-(void) DrawRect: (cgrect) rect {[Super Drawrect:rect]; CGRect frame = CGRectMake (50, 100, 100, 100); /* Draw filled Circle */cgcontextref context = Uigraphicsgetcurrentcontext (); [[Uicolor Whitecolor] set]; Cgcontextfillrect (context, rect); Cgcontextaddellipseinrect (context, frame); [[Uicolor Orangecolor] set]; Cgcontextfillpath (context); /* Border Circle */cgcontextsetrgbstrokecolor (context, 255/255.0, 106/255.0, 0/255.0, 1); Cgcontextsetlinewidth (context, 5); Cgcontextaddarc (context, 0, 2*M_PI, 0); Cgcontextdrawpath (context, kcgpathstroke); /* Draw Straight line */CGRect markframe = CGRectMake (100, 250, 200, 200); [[Uicolor Orangecolor] set]; Cgcontextsetlinewidth (context, 4); Cgpoint Points[5]; Points[0] = Cgpointmake (MARKFRAME.ORIGIN.X,MARKFRAME.ORIGIN.Y); POINTS[1] = Cgpointmake (MARKFRAME.ORIGIN.X+MARKFRAME.SIZE.WIDTH/4, MARKFRAME.ORIGIN.Y+MARKFRAME.SIZE.HEIGHT/2); POINTS[2] = Cgpointmake (markframe.origin.x+markframe.sIZE.WIDTH/2, markframe.origin.y+5); POINTS[3] = Cgpointmake (MARKFRAME.ORIGIN.X+MARKFRAME.SIZE.WIDTH/4*3,MARKFRAME.ORIGIN.Y+MARKFRAME.SIZE.HEIGHT/2); POINTS[4] = Cgpointmake (Markframe.origin.x+markframe.size.width, MARKFRAME.ORIGIN.Y); Cgcontextaddlines (context, points, 5); Cgcontextdrawpath (context, kcgpathstroke); Border Circle Cgcontextsetlinewidth (context, 5); Cgcontextaddarc (context, MARKFRAME.ORIGIN.X+MARKFRAME.SIZE.WIDTH/2, MARKFRAME.ORIGIN.Y+MARKFRAME.SIZE.HEIGHT/2, Markframe.size.height/2-2, 0, 2*M_PI, 0); Cgcontextdrawpath (context, kcgpathstroke); /* curve */[[Uicolor Redcolor] set]; Cgcontextsetlinewidth (context, 4.0); Cgcontextsetstrokecolorwithcolor (context, [Uicolor Orangecolor]. Cgcolor); Cgcontextmovetopoint (context, 300, 370); Cgcontextaddcurvetopoint (context, 193, 320, 100, 370, 100, 370); Cgcontextstrokepath (context);}
Uiviewcontroller:
#import "DrawViewController.h" @interface Drawviewcontroller () @end @implementation drawviewcontroller-(void) viewdidload {[Super viewdidload]; /* * Draw a dashed circle */Cashapelayer *dotteline = [Cashapelayer layer]; Cgmutablepathref Dottepath = cgpathcreatemutable (); Dotteline.linewidth = 2.0f; Dotteline.strokecolor = [Uicolor Orangecolor]. Cgcolor; Dotteline.fillcolor = [Uicolor Clearcolor]. Cgcolor; Cgpathaddellipseinrect (Dottepath, Nil, CGRectMake (50.0f, 50.0f, 200.0f, 200.0f)); Dotteline.path = Dottepath; Nsarray *arr = [[Nsarray alloc] Initwithobjects:[nsnumber numberwithint:10],[nsnumber Numberwithint:5], nil]; Dotteline.linedashphase = 1.0; Dotteline.linedashpattern = arr; Cgpathrelease (Dottepath); [Self.view.layer Addsublayer:dotteline]; /* * Draw solid line round */cashapelayer *solidline = [Cashapelayer layer]; Cgmutablepathref Solidpath = cgpathcreatemutable (); Solidline.linewidth = 2.0f; Solidline.strokecolor = [Uicolor orAngecolor]. Cgcolor; Solidline.fillcolor = [Uicolor Clearcolor]. Cgcolor; Cgpathaddellipseinrect (Solidpath, Nil, CGRectMake (50.0f, 300.0f, 200.0f, 200.0f)); Solidline.path = Solidpath; Cgpathrelease (Solidpath); [Self.view.layer Addsublayer:solidline]; /* * Draw a dashed line */cashapelayer *dotteshapelayer = [Cashapelayer layer]; Cgmutablepathref Dotteshapepath = cgpathcreatemutable (); [Dotteshapelayer Setfillcolor:[[uicolor Clearcolor] cgcolor]; [Dotteshapelayer Setstrokecolor:[[uicolor Orangecolor] cgcolor]; Dotteshapelayer.linewidth = 2.0f; Nsarray *dotteshapearr = [[Nsarray alloc] Initwithobjects:[nsnumber numberwithint:10],[nsnumber NumberWithInt:5], nil] ; [Dotteshapelayer Setlinedashpattern:dotteshapearr]; Cgpathmovetopoint (Dotteshapepath, NULL, 20,500); Cgpathaddlinetopoint (Dotteshapepath, NULL, 20, 285); Cgpathaddlinetopoint (Dotteshapepath, NULL, 300,285); [Dotteshapelayer Setpath:dotteshapepath]; Cgpathrelease (Dotteshapepath); [Self.view.layer Addsublayer:dotteshapelayer]; /* * Draw solid line */Cashapelayer *solidshapelayer = [Cashapelayer layer]; Cgmutablepathref Solidshapepath = cgpathcreatemutable (); [Solidshapelayer Setfillcolor:[[uicolor Clearcolor] cgcolor]; [Solidshapelayer Setstrokecolor:[[uicolor Orangecolor] cgcolor]; Solidshapelayer.linewidth = 2.0f; Cgpathmovetopoint (Solidshapepath, NULL, 20, 265); Cgpathaddlinetopoint (Solidshapepath, NULL, 300,265); Cgpathaddlinetopoint (Solidshapepath, NULL, 300,50); [Solidshapelayer Setpath:solidshapepath]; Cgpathrelease (Solidshapepath); [Self.view.layer Addsublayer:solidshapelayer]; }
Illustration:
iOS draw circles, draw lines