iOS Development UI Chapter-quartz2d use (drawing path)

Source: Internet
Author: User

One, drawing path

A. Brief descriptionWhen you draw a line, the inside of the method creates a path by default. It puts the paths in the path. 1. Create a path Cgmutablepathref calling this method is equivalent to creating a path that holds the drawing information. 2. Add the drawing information inside the path. The previous method is to add the point location to the CTX (graphical context information), and CTX creates a path internally to hold the drawing information by default. In the graphical context, there is a piece of storage space dedicated to storing drawing information, in fact, this space is cgmutablepathref. 3. Add the path to the context. code example: Drawing a line's code:
1     //1. Get the Graphics Context 2     cgcontextref Ctx=uigraphicsgetcurrentcontext () 3     //2. Drawing (Draw line) 4     //Set starting point 5     Cgcontextmovetopoint (CTX, 8); 6     //Set End 7     cgcontextaddlinetopoint (CTX, 9)     Cgcontextstrokepath (CTX);

The above code is equivalent to the following code.

1     //1. Get the Graphics Context 2     cgcontextref ctx=uigraphicsgetcurrentcontext (); 3      4     //2. Drawing 5     //2.1 creating a straight line drawing path 6     //Note: Any value created with the Creat/copy/retain method in quartz2d must be freed by 7     Cgmutablepathref path=cgpathcreatemutable (); 8     //2.2 Add the drawing information to the path 9     cgpathmovetopoint (path, NULL,     200, 300); Cgpathaddlinetopoint (path, NULL, One     by one//2.3 add the path to the context     . Save the drawing information of the drawing line to the graphical context of     Cgcontextaddpath (CTX, path);     //3. Render     Cgcontextstrokepath (CTX);     //4. Releases the two paths created earlier     //The first method is     cgpathrelease (path); //     /The second method is     //    cfrelease (path); 23}
B. Benefits of using path directly:The first code reading is not good, not easy to distinguish. With path, a path represents a route. For example, if you want to draw multiple graphs in context, it is recommended that you use path. code example:
 1-(void) DrawRect: (cgrect) Rect 2 {3//1. Get the Graphics Context 4 cgcontextref ctx=uigraphicsgetcurrentcontext (); 5 6//2. Drawing 7//2.a Draw a line 8//2.a.1 create a path to a drawing 9//Note: Any value created with the Creat/copy/retain method in quartz2d must be released in Cgmutablepathr EF path=cgpathcreatemutable ();//2.a.2 add the drawing information to the path in Cgpathmovetopoint (path, NULL, ()); Cgpathad  Dlinetopoint (Path, NULL, $);//2.a.3 add a path to context 17//Save the drawing information of the drawing line in the context of the drawing Cgcontextaddpath (CTX, path);//2.b Draw a circle//2.b.1 create a drawing path of a circle (note that this is mutable, not cgpathref) Cgmutablepathref PATH1=CGPATHCR Eatemutable ();//2.b.2 Add the circular drawing information to the path of Cgpathaddellipseinrect (path1, NULL, CGRectMake (50, 50, 100, 100)); 2 7//2.b.3 Add the circle path to the graphics context Cgcontextaddpath (CTX, path1); 30 31 32//3. Render the Cgcontextstrokepat H (CTX); 34 35//4. Release the two paths created previously 36//The first method is Cgpathrelease (path), Cgpathrelease (path1), 39//The second method 40 Cfrelease (path); 41 } 
Effect: Tip: If you are drawing a line, create a path (path) to hold the drawing information for the drawing line, and if you want to redraw a circle again, you can create a new path to save the drawing information of the circle specifically. Note: Any value created with the Creat/copy/retain method in quarzt2d must be manually released there are two ways to free the previously created path: (1) cgpathrelease (Path), (2) cfrelease (path); Description: Cfrelease belongs to the lower Cocafoundation framework second, supplementary knowledge Points: Draw the four-sided shape of some methods: the first way: by connecting fixed points to draw the quadrilateral second way: Specify the starting point and the width of the height of the third way: The second way to merge two steps into one step. Fourth Way (OC method): Draw a solid quadrilateral, notice there is no hollow method fifth: Draw the root line, set the width of the line (in this way you can draw the diagonal quadrilateral) code example:
 1//2//YYVIEW.M 3//06-Quadrilateral Five Representations 4//5//Created by Apple on 14-6-11. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYview.h" @implementation YYview12-(void) DrawRect: (CGRect) rect15 {16//Get Graphics context C    Gcontextref Ctx=uigraphicsgetcurrentcontext (); 18//The first drawing, drawn by connecting a fixed point to the quadrilateral//Cgcontextmovetopoint (CTX, 0, 20); 20// Cgcontextaddlinetopoint (< #CGContextRef C#>, < #CGFloat X#>, < #CGFloat y#>);//Cgcontextaddlineto Point (< #CGContextRef C#>, < #CGFloat X#>, < #CGFloat y#>);//Cgcontextaddlinetopoint (<# Cgcontextref C#>, < #CGFloat X#>, < #CGFloat y#>); 23 24///The second way: Specify the starting point and the width of the height of the drawing quadrilateral//Cgcontextaddrect (CTX, CGRectMake (20, 20, 200, 100)); 26////rendering//Cgcontextstrokepath (CTX); 28 29//Third Way: Two two-step merge into one step. 30//Draw hollow quadrilateral//Cgcontextstrokerect (CTX, CGRectMake (20, 20, 200, 100)), 32//////////Cgcontextfillre CT (CTX, CGRectMake (20, 20, 200, 100)); 34 35//Fourth Way (OC method): Draw solid quadrilateral, note no Hollow method Uirectfill (CGRectMake (20, 20, 200, 100)); 37 38// Five ways: Draw the root line, set the width of the line (in this way you can draw a diagonal quadrilateral)//Cgcontextmovetopoint (CTX, 100, 200); ; Cgcontextsetlinewidth (CTX, 50); 42////Note that the line can only be drawn as hollow//Cgcontextstrokepath (CTX);}46 @end

The fifth method is to draw a diagonal quadrilateral.

iOS Development UI Chapter-quartz2d use (drawing path)

Related Article

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.