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 context2Cgcontextref CTX =Uigraphicsgetcurrentcontext ();3     // 2. Drawing (draw line)4     // set the starting point5Cgcontextmovetopoint (CTX, -, -);6     // Set End point7Cgcontextaddlinetopoint (CTX, $, -);8     // Rendering9Cgcontextstrokepath (CTX);

The above code is equivalent to the following code.

1     // 1. Get the graphics context2Cgcontextref CTX =Uigraphicsgetcurrentcontext ();3     4     // 2. Drawing5     // 2.1 Creating a path to a straight line drawing6     // Note: Any value created with the Creat/copy/retain method in quartz2d must be released7Cgmutablepathref Path =cgpathcreatemutable ();8     // 2.2 Adding the drawing information to the path9Cgpathmovetopoint (Path, NULL, -, -);TenCgpathaddlinetopoint (Path, NULL, $, -); One     // 2.3 Add a path to the context A     // Save the drawing information of the drawing line in the graphics context - Cgcontextaddpath (CTX, path); -      the     // 3. Rendering - Cgcontextstrokepath (CTX); -      -     // 4. Release the two paths created earlier +     //The first of these methods - cgpathrelease (path); +     //The second method of A//cfrelease (path); at}
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) Rect2 {3     // 1. Get the graphics context4Cgcontextref CTX =Uigraphicsgetcurrentcontext ();5 6     // 2. Drawing7     // 2.a draw a straight line8     // 2.a.1 Creating a path to a drawing9     // Note: Any value created with the Creat/copy/retain method in quartz2d must be releasedTenCgmutablepathref Path =cgpathcreatemutable (); One      A     // 2.a.2 Adding the drawing information to the path -Cgpathmovetopoint (Path, NULL, -, -); -Cgpathaddlinetopoint (Path, NULL, $, -); the      -     // 2.a.3 Adding a path to the context -     // Save the drawing information of the drawing line in the graphics context - Cgcontextaddpath (CTX, path); +      -      +     // 2.b Draw a circle A     // 2.b.1 Create a circle drawing path (Note that this is mutable, not cgpathref) atCgmutablepathref path1=cgpathcreatemutable (); -      -     //2.b.2 to add a circle of drawing information to the path -Cgpathaddellipseinrect (path1, NULL, CGRectMake ( -, -, -, -)); -      -     //2.b.3 Add a circle path to the graphics context in Cgcontextaddpath (CTX, path1); -      to      +     //3. Rendering - Cgcontextstrokepath (CTX); the      *     //4. Release the two paths created earlier $     //The first of these methodsPanax Notoginseng cgpathrelease (path); - cgpathrelease (path1); the     //The second method of + //cfrelease (path);cfrelease//(path1);
42}
Effect:Tips:If you are drawing a line, create a 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 released manually 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:Some ways to draw quads: The first way: to draw a quadrilateral by connecting a fixed point the second way: to specify the starting point and the width height to draw the quadrilateral Third way: combine two steps in the second way into one step. The Fourth Way (OC method):draw a solid quadrilateral, note that there is no hollow methodFifth: Draw the root line, set the width of the line (in this way you can draw a diagonal quadrilateral) code example:
1 //2 //yyview.m3 //06-Five kinds of representations of quads4 //5 //Created by Apple on 14-6-11.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYView.h"Ten  One @implementationYyview A  -  -- (void) DrawRect: (cgrect) Rect the { -     // Get the graphics context -Cgcontextref CTX =Uigraphicsgetcurrentcontext (); -     // The first way of drawing a quadrilateral by connecting a fixed point + //cgcontextmovetopoint (CTX, 0,); - //Cgcontextaddlinetopoint (< #CGContextRef C#>, < #CGFloat X#>, < #CGFloat y#>); + //Cgcontextaddlinetopoint (< #CGContextRef C#>, < #CGFloat X#>, < #CGFloat y#>); A //Cgcontextaddlinetopoint (< #CGContextRef C#>, < #CGFloat X#>, < #CGFloat y#>); at      -     // Second way: Specify the starting point and the width height to draw the quadrilateral - //Cgcontextaddrect (CTX, CGRectMake (); - //    // Rendering - //Cgcontextstrokepath (CTX); -      in     // The Third way: two kinds of two steps to merge into one step.  -     // to draw a hollow quadrilateral to //Cgcontextstrokerect (CTX, CGRectMake (); + //    // to draw a solid quadrilateral - //Cgcontextfillrect (CTX, CGRectMake (); the      *     // Fourth Way (OC method): Draw solid quads, notice no hollow method $Uirectfill (CGRectMake ( -, -, $, -));Panax Notoginseng      -     // Fifth way: Draw the root line, set the width of the line (in this way you can draw the diagonal quadrilateral) the //Cgcontextmovetopoint (CTX, a); + //Cgcontextaddlinetopoint (CTX, n, +); A //cgcontextsetlinewidth (CTX,); the //    //Note that the lines can only be painted hollow. + //Cgcontextstrokepath (CTX); -      $ } $ @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.