From: http://blog.sina.com.cn/s/blog_79a120e501018c2n.html
Uiview method:
-(Void) drawrect :( cgrect) rect
Obtain the current context:
Cgcontextref context = uigraphicsgetcurrentcontEXT ();
Get the starting point and size of the current bounds:
Self. bounds. Origin
Self. bounds. Size
Set the line width and fill line color of the drawing:
Cgcontextsetlinewidth (context, 5.0 );
[[Uicolor bluecolor] setstroke];
Additional call Method for drawing:
You need to push the context before using the context, and then pop it. In this way, the context outside will not be damaged, and I will do anything in it.
Uigraphicspushcontext (context );
Cgcontextbeginpath (context );
Cgcontextaddarc (context, p. x, P. Y, radius, 0, 2 * m_pi, yes );
// Cgcontextaddcurvetopoint(Context, mouthcp1.x, mouthcp1.y, mouthcp2.x, mouthcp2.y, // mouthend. X, mouthend. Y) are the same routines ..
Cgcontextstrokepath (context );
Uigraphicspopcontext ();
Inherit uiview and convert uiview into sub-view. Uibezierpath class drawing. Drawing with rectangular coordinates
-(Void) drawrect :( cgrect) rect
{
// Rectangular drawing Area
Cgrect arectangle = cgrectmake (0.0,
0.0, 40.0, 40.0 );
// Define a rectangular path
Uibezierpath * Path = [uibezierpath bezierpathwithrect: arectangle];
// Draw the rectangular path
[Pathstroke];
// Draw an ellipse in the area
Path = [Uibezierpathbezierpathwithovalinrect: Arectangle];
// Elliptical Filling
[Pathfill];
// Define a start path
Uibezierpath * startpath = [uibezierpath bezierpath];
// Start to draw a star
[Startpathmovetopoint: cgpointmake (40.0,
(0.0)];
[StartpathAddlinetopoint: cgpointmake (30.0,
(30.0)];
[StartpathAddlinetopoint: cgpointmake (0.0,
(30.0)];
[StartpathAddlinetopoint: cgpointmake (20.0,
(50.0)];
[StartpathAddlinetopoint: cgpointmake (10.0,
(80.0)];
[StartpathAddlinetopoint: cgpointmake (40.0,
(60.0)];
[StartpathAddlinetopoint: cgpointmake (70.0,
(80.0)];
[StartpathAddlinetopoint: cgpointmake (60.0,
(50.0)];
[StartpathAddlinetopoint: cgpointmake (80.0,
(30.0)];
[StartpathAddlinetopoint: cgpointmake (50.0,
(30.0)];
[Startpathclosepath];
// Obtain the current environment
Cgcontextref context = uigraphicsgetcurrentcontEXT ();
// Save the current environment for future recovery
Cgcontextsavegstate (context );
// Change the starting point of the coordinate to (100,100)
Cgcontexttranslatectm (context,
100,100 );
// Turns the current color into yellow
Uicolor * fillcolor = [uicolor yellowcolor];
[Fillcolorsetfill];
// The star is yellow.
[Startpathfill];
// Change the starting point of the coordinate to the starting point of (100,100). Note: This is the relative displacement of the current (100,100), so it is (200,200) relative to the entire view)
Cgcontexttranslatectm (context,
100,100 );
// Rotate coordinates 45 degrees
Cgcontextrotatectm (context,
3.14/4 );
// Change the current color to green
Fillcolor = [uicolor
Greencolor];
[Fillcolorsetfill];
// Draw the outline of the pentagram into a color
[Startpathstroke];
// Star filling
[Startpathfill];
// Restore the context. Of course, it is the one saved before the restoration.
Cgcontextrestoregstate (context );
// Save the current context
Cgcontextsavegstate (context );
Cgcontexttranslatectm (context,
50,250 );
[Startpathfill];
Cgcontextrestoregstate (context );
}