iOS Development UI Chapter-quartz2d use (Matrix operations)

Source: Internet
Author: User

iOS Development UI Chapter-quartz2d use (Matrix operations)

first, about the matrix operation 1. Draw a quadrilateralA four-sided drawing is done by setting two endpoints (length and width). Code:
-(void) DrawRect: (cgrect) rect{    // draw quadrilateral     // Get Graphics context    cgcontextref ctx=uigraphicsgetcurrentcontext ();     // Drawing    Cgcontextaddrect (CTX, CGRectMake ());     // Rendering     Cgcontextstrokepath (CTX);}

Description:There are weaknesses in drawing rectangles in this way:The drawn rectangle is always positive. such as:

2. Draw a crooked quadrilateralHow to draw a crooked rectangle? (Done with a matrix operation, similar to a deformation operation) can be manipulated by the matrix to deform (rotate, scale, pan) the object of the drawing (rotation, scaling, panning) method:CGCONTEXTROTATECTM (< #CGContextRef C#>, < #CGFloat angle#>) This accepts two parameters (graphics context, radians)watch out.:Setting the matrix operation must be done before adding the graphic, if it is finished after adding the graphic, it is not valid at this time. Code:
- (void) DrawRect: (cgrect) rect{//Draw Quadrilateral//Get the graphics contextCgcontextref ctx=Uigraphicsgetcurrentcontext (); //Matrix Operations//Note: The set matrix operation must be preceded by the addition of the drawing information.//Rotate 45 degreesCGCONTEXTROTATECTM (CTX, m_pi_4); //DrawingCgcontextaddrect (CTX, CGRectMake ( Max, -, -, -)); //RenderingCgcontextstrokepath (CTX);}
Effect:

Ii. about rotation 1. Rotate the demo view to display the view because it has a layer above it and the future graph is rendered to the layer. And, when rotating the entire layer is rotated, you can draw a circle to verify. Code 1 (not rotated):
- (void) DrawRect: (cgrect) rect{//Get the graphics contextCgcontextref ctx=Uigraphicsgetcurrentcontext (); //Matrix Operations//Note: The set matrix operation must be preceded by the addition of the drawing information.//Rotate 45 degrees//CGCONTEXTROTATECTM (CTX, m_pi_4); //Drawing//Draw QuadrilateralCgcontextaddrect (CTX, CGRectMake ( Max, -, -, -)); //Draw a circleCgcontextaddellipseinrect (CTX, CGRectMake ( $, $, -, -)); //RenderingCgcontextstrokepath (CTX);}
Effect:

Code 2 (rotation):

- (void) DrawRect: (cgrect) rect{//Get the graphics contextCgcontextref ctx=Uigraphicsgetcurrentcontext (); //Matrix Operations//Note: The set matrix operation must be preceded by the addition of the drawing information.//Rotate 45 degreesCGCONTEXTROTATECTM (CTX, m_pi_4); //Drawing//Draw QuadrilateralCgcontextaddrect (CTX, CGRectMake ( Max, -, -, -)); //Draw a circleCgcontextaddellipseinrect (CTX, CGRectMake ( $, $, -, -)); //RenderingCgcontextstrokepath (CTX);}

Effect:

2. Additional Notes on rotationTips:When rotated, the entire layer is rotated. Three, the scaling method:CGCONTEXTSCALECTM (< #CGContextRef C#>, < #CGFloat Sx#>, < #CGFloat sy#>)The method receives three parameters (the graph context, the X-direction scale, and the scaling code example in the y direction):
- (void) DrawRect: (cgrect) rect{//Get the graphics contextCgcontextref ctx=Uigraphicsgetcurrentcontext (); //Matrix Operations//Note: The set matrix operation must be preceded by the addition of the drawing information.//Scaling , X-direction scaling 0.5 times times, y-direction scaling 1.5 times timesCGCONTEXTSCALECTM (CTX,0.5,1.5); //Drawing//Draw QuadrilateralCgcontextaddrect (CTX, CGRectMake ( Max, -, -, -)); //Draw a circleCgcontextaddellipseinrect (CTX, CGRectMake ( $, $, -, -)); //RenderingCgcontextstrokepath (CTX);}

Effect:

Four, the translation method:CGCONTEXTTRANSLATECTM (< #CGContextRef C#>, < #CGFloat Tx#>, < #CGFloat ty#>)The method receives three parameters (graph context, offset in x direction, offset in y direction) code example:
- (void) DrawRect: (cgrect) rect{//Get the graphics contextCgcontextref ctx=Uigraphicsgetcurrentcontext (); //Matrix Operations//Note: The set matrix operation must be preceded by the addition of the drawing information.//Shift , X-direction move 50,y directionCGCONTEXTTRANSLATECTM (CTX, -, -); //Drawing//Draw QuadrilateralCgcontextaddrect (CTX, CGRectMake ( Max, -, -, -)); //Draw a circleCgcontextaddellipseinrect (CTX, CGRectMake ( $, $, -, -)); //RenderingCgcontextstrokepath (CTX);}

Effect:

Tip: The coordinate origin is the upper-left corner of the view.

iOS Development UI Chapter-quartz2d use (Matrix operations)

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.