iOS Development UI Chapter-quartz2d use (drawing path)

Source: Internet
Author: User

iOS Development UI Chapter-quartz2d use (drawing path)

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. Get the graphics context    Cgcontextref ctx=uigraphicsgetcurrentcontext ();     // 2. Drawing (Draw line)     // set the starting point     -  - );     // set end point     $  - );     // Rendering    Cgcontextstrokepath (CTX);

The above code is equivalent to the following code.

//1. Get the graphics contextCgcontextref ctx=Uigraphicsgetcurrentcontext (); //2. Drawing//2.1 Creating a path to a straight line drawing//Note: Any value created with the Creat/copy/retain method in quartz2d must be releasedCgmutablepathref path=cgpathcreatemutable (); //2.2 Adding the drawing information to the pathCgpathmovetopoint (Path, NULL, -, -); Cgpathaddlinetopoint (Path, NULL, $, -); //2.3 Add a path to the context//Save the drawing information of the drawing line in the graphics contextCgcontextaddpath (CTX, path); //3. RenderingCgcontextstrokepath (CTX); //4. Release the two paths created earlier//The first of these methodscgpathrelease (path); //The second method of//cfrelease (path);}

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:
- (void) DrawRect: (cgrect) rect{//1. Get the graphics contextCgcontextref ctx=Uigraphicsgetcurrentcontext (); //2. Drawing//2.a draw a straight line//2.a.1 Creating a path to a drawing//Note: Any value created with the Creat/copy/retain method in quartz2d must be releasedCgmutablepathref path=cgpathcreatemutable (); //2.a.2 Adding the drawing information to the pathCgpathmovetopoint (Path, NULL, -, -); Cgpathaddlinetopoint (Path, NULL, $, -); //2.a.3 Adding a path to the context//Save the drawing information of the drawing line in the graphics contextCgcontextaddpath (CTX, path); //2.b Draw a circle//2.b.1 Create a circle drawing path (Note that this is mutable, not cgpathref)Cgmutablepathref path1=cgpathcreatemutable (); //2.b.2 to add a circle of drawing information to the pathCgpathaddellipseinrect (path1, NULL, CGRectMake ( -, -, -, -)); //2.b.3 Add a circle path to the graphics contextCgcontextaddpath (CTX, path1); //3. RenderingCgcontextstrokepath (CTX); //4. Release the two paths created earlier//The first of these methodscgpathrelease (path);    Cgpathrelease (path1); //The second method of//cfrelease (path);}
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: 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:
////yyview.m//06-Five kinds of representations of quads////Created by Apple on 14-6-11.//Copyright (c) 2014 itcase. All rights reserved.//#import"YYview.h"@implementation Yyview- (void) DrawRect: (cgrect) rect{//Get the graphics contextCgcontextref 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#>);//Cgcontextaddlinetopoint (< #CGContextRef C#>, < #CGFloat X#>, < #CGFloat y#>); //Second way: Specify the starting point and the width height to draw the quadrilateral//Cgcontextaddrect (CTX, CGRectMake ();//    //Rendering//Cgcontextstrokepath (CTX); //The Third way: two kinds of two steps to merge into one step. //to draw a hollow quadrilateral//Cgcontextstrokerect (CTX, CGRectMake ();//    //to draw a solid quadrilateral//Cgcontextfillrect (CTX, CGRectMake (20, 20, 200, 100)); //Fourth Way (OC method): Draw solid quads, notice no hollow methodUirectfill (CGRectMake ( -, -, $, -)); //Fifth way: Draw the root line, set the width of the line (in this way you can draw the diagonal quadrilateral)//Cgcontextmovetopoint (CTX, a);//Cgcontextaddlinetopoint (CTX, n, +);//cgcontextsetlinewidth (CTX,);//    //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)

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.