iOS Development UI chapter-quartz2d simple use (i)

Source: Internet
Author: User
<span id="Label3"></p><p><p>first, draw a straight line</p></p><p><p>Code:</p></p><pre> 1//2//YYLINEVIEW.M 3//03-draw line 4//5//Created by Apple on 14-6-9. 6//Copyright (c) 2014 Itcase. All Rights Reserved. 7//8 9 #import "YYlineview.h" @implementation YYlineview12 13 14//when The custom view is first displayed, the DrawRect method is called (void) DRA Wrect: (cgrect) rect16 {17 18//1. Gets the graphics context associated with the current view (because The graphics context determines the output target to draw)/19//if it is called in the DrawRect method Uigraphicsgetcurrentcon The text method gets out of the context of layer Cgcontextref Ctx=uigraphicsgetcurrentcontext ();//do not need *, with Id21 22//2. drawing (drawing lines), saving drawing information 23//set starting point cgcontextmovetopoint (ctx, 20, 100); 25//set end point Cgcontextaddlinetopoint (ctx, 300, 100); 27 28 29//set The state of the drawing 30//set the color of the line to Blue Cgcontextsetrgbstrokecolor (ctx, 0, 1.0, 0, 1.0); 32//set The width of the line CG Contextsetlinewidth (ctx, 15); 34//set the line start and end style to rounded corners Cgcontextsetlinecap (ctx, kcglinecapround); 36//set The style of the line's corners as Rounded Cgcontextsetlinejoin (ctx, kcglinejoinround); 38//3. render (draw a hollow line) cgcontextstrokepath (ctx); 40 41// Note that lines cannot be rendered asSolid Cgcontextfillpath (ctx); 43 44 45 46//set second line 47//set the beginning of the second line Cgcontextmovetopoint (ctx, 50, 200); 49//set The end of the second antenna (automatically the end of the previous line as the starting Point) cgcontextaddlinetopoint (ctx, 300, 60); 51 52//set The status of the Drawing//C Gcontextsetrgbstrokecolor (ctx, 1.0, 0.7, 0.3, 1.0); 54//the Second way to set the color is [[uicolor graycolor] set];56//setting the width of the line 57 Cgcontextsetlinewidth (ctx, 10); 58//set the start and end style of the line Cgcontextsetlinecap (ctx, kcglinecapbutt); 60 61//shading Dye the second line of graphics to view 62//draw a hollow line of Cgcontextstrokepath (ctx);}65 @end</pre><p><p>Effect:</p></p><p><p></p></p><p><p>two, Draw a triangle</p></p><p><p>Code:</p></p><pre><pre>1//2// yyrectview.m 3// 02-draw triangle 4//5// Created by Hole doctor on 14-6-10.6// Copyright (c) 2014 Itcast. All Rights Reserved. 7//8 9 #import "YYrectview.h" @implementation YYrectview12-(void) drawrect: (cgrect) rect15 { //1. get Graphical context cgcontextref ctx=uigraphicsgetcurrentcontext (); //2. draw triangle //set starting point Cgcontextmovetopoint (ctx, +);/ /set The second point at cgcontextaddlinetopoint (ctx, +, +); Set the third point cgcontextaddlinetopoint (ctx, n, x); +/ /set end point// cgcontextaddlinetopoint (ctx, 20, 100) ;/ /close start and end of cgcontextclosepath (ctx); 3. Render the drawing to layer on the Cgcontextstrokepath ( ctx): }35-notoginseng @end</pre></pre><p><p>Effect:</p></p><p><p></p></p><p><p>Tip: close the start and end Cgcontextclosepath (ctx);</p></p><p><p>three, Draw Quadrilateral</p></p><p><p>Code:</p></p><pre><pre> 1//2//yyrect.m 3//03-draw quadrilateral 4//5//Created by Hole doctor on 14-6-10.6//Copyright (c) 2014 Itcast. All Rights Reserved. 7//8 9 #import "YYrect.h" @implementation YYrect12-(void) drawrect: (cgrect) rect15 {16 17//1. Get the graphics context 18 Cgcontextref Ctx=uigraphicsgetcurrentcontext (); 19//2. draw quadrilateral Cgcontextaddrect (ctx, cgrectmake (20, 20, 150, 100 )); 21 22//if you want to set the state of the drawing must be before rendering At//cgcontextsetrgbstrokecolor (ctx, 1.0, 0, 0, 1.0); 24//draw What type of graphic (hollow or solid). what type of method will be used to set the status Of//cgcontextsetrgbfillcolor (ctx, 1.0, 0, 0, 1.0); 26 27//call oc method to set the color of the drawing 28// [[uicolor purplecolor] setfill];29//[[uicolor bluecolor] setstroke];30//call oc Method set drawing color (both solid and Hollow) 31// [[uicolor greencolor] set];32 [[uicolor colorwithred:1.0 green:0 blue:0 alpha:1.0] set];33 34 35//3. rendering Graphics to layer 36//hollow PNS cgcontextstrokepath (ctx), 38//solid cgcontextfillpath (ctx),}42 @end /pre></pre></pre><p><p>Tip: If you want to set the state of the drawing, it must precede rendering.</p></p><p><p>Effects (solid and hollow):</p></p><p><p></p></p><p><p>four, Draw a circle</p></p><p><p>Code Listing 1:</p></p><pre><pre>-(void) drawrect: (cgrect) rect{ //1. Get context cgcontextref CTX = Uigraphicsgetcurrentcontext (); Draw round Cgcontextaddarc (ctx, 0, 2 * m_pi, 0); 3. Rendering (note that the line can only be drawn through the hollow)// Cgcontextfillpath (ctx); Cgcontextstrokepath (ctx); }</pre></pre><p><p>Effect:</p></p><p><p></p></p><p><p>Code Listing 2:</p></p><pre><pre>Draw a circle //1. get context cgcontextref CTX = Uigraphicsgetcurrentcontext (); 2. Draw round cgcontextaddellipseinrect (ctx, cgrectmake (): [[uicolor greencolor] set]; 3. Render // Cgcontextstrokepath (ctx); Cgcontextfillpath (ctx);</pre></pre><p><p>Effect:</p></p><p><p></p></p><p><p>Code Listing 3:</p></p><pre><pre>Draw Ellipse //1. Get context cgcontextref CTX = Uigraphicsgetcurrentcontext (); 2. Draw round cgcontextaddellipseinrect (ctx, cgrectmake (+, +, +)); [[uicolor purplecolor] set]; 3. Render // Cgcontextstrokepath (ctx); Cgcontextfillpath (ctx);</pre></pre><p><p>Effect:</p></p><p><p></p></p><p><p>five, Draw Arc</p></p><p><p>Code Listing 1:</p></p><pre><pre> Draw Arc //1. Get context cgcontextref CTX = Uigraphicsgetcurrentcontext (); 2. Draw Arc //x/y Center //radius radius //startangle start radian //endangle end of Arc ///clockwise Draw arc Direction (0 clockwise, 1 Counterclockwise) // cgcontextaddarc (ctx,-m_pi_2, m_pi_2, 0); Cgcontextaddarc (ctx, m_pi_2, m_pi, 0); Cgcontextclosepath (ctx); 3. Render // Cgcontextstrokepath (ctx); Cgcontextfillpath (ctx);</pre></pre><p><p>Effect:</p></p><p><p></p></p><p><p>Code Listing 2:</p></p><pre><pre> 1. Get context cgcontextref CTX = Uigraphicsgetcurrentcontext (); 2. appeased map //draw line cgcontextmovetopoint (ctx,, +); Cgcontextaddlinetopoint (ctx, +,); Draw arc Cgcontextaddarc (ctx, m_pi_2, m_pi, 0); cgcontextaddarc (ctx,-m_pi, m_pi_2, 1); Close path cgcontextclosepath (ctx); [[uicolor browncolor]set]; 3. Rendering (note that the line can only be painted by Hollow) Cgcontextfillpath (ctx); Cgcontextstrokepath (ctx);</pre></pre><p><p>Effect:</p></p><p><p></p></p><p><p>iOS Development UI chapter-quartz2d simple use (i)</p></p></span>

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.