"IOS" quartz2d matrix operations

Source: Internet
Author: User

In front of the drawing of basic graphics, the four-sided shape is composed of a few straight lines, now there is a more convenient method.

first, about the matrix operation1. Draw a quadrilateralA four-sided drawing is done by setting two endpoints (length and width). Code:
1- (void) DrawRect: (cgrect) Rect2 {3     //Draw Quadrilateral4     //Get the graphics context5Cgcontextref ctx=Uigraphicsgetcurrentcontext ();6     //Drawing7Cgcontextaddrect (CTX, CGRectMake ( -, -, -, -));8     //Rendering9 Cgcontextstrokepath (CTX);Ten}
View CodeDescription: Draw a rectangle in this way with a weakness: The drawn rectangle is alwaysIt's all squares .。 such as: How 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 drawing of Things: CGCONTEXTROTATECTM (< #CGContextRef C#>, < #CGFloat angle#>) should accept two parameters (graphics context, radians)Note: 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:
1- (void) DrawRect: (cgrect) Rect2 {3     //Draw Quadrilateral4     //Get the graphics context5Cgcontextref ctx=Uigraphicsgetcurrentcontext ();6     //Matrix Operations7     //Note: The set matrix operation must be preceded by the addition of the drawing information.8     //Rotate 45 degrees9 CGCONTEXTROTATECTM (CTX, m_pi_4);Ten      One     //Drawing ACgcontextaddrect (CTX, CGRectMake ( Max, -, -, -)); -     //Rendering - Cgcontextstrokepath (CTX); the}
View CodeEffect:second, about rotation1. 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):
1- (void) DrawRect: (cgrect) Rect2 {3     //Get the graphics context4Cgcontextref ctx=Uigraphicsgetcurrentcontext ();5     //Matrix Operations6     //Note: The set matrix operation must be preceded by the addition of the drawing information.7     //Rotate 45 degrees8     //CGCONTEXTROTATECTM (CTX, m_pi_4);9     Ten     //Drawing One     //Draw Quadrilateral ACgcontextaddrect (CTX, CGRectMake ( Max, -, -, -)); -     //Draw a circle -Cgcontextaddellipseinrect (CTX, CGRectMake ( $, $, -, -)); the     //Rendering - Cgcontextstrokepath (CTX); -}
View CodeEffect: Code 2 (rotation):
1- (void) DrawRect: (cgrect) Rect2 {3     //Get the graphics context4Cgcontextref ctx=Uigraphicsgetcurrentcontext ();5     //Matrix Operations6     //Note: The set matrix operation must be preceded by the addition of the drawing information.7     //Rotate 45 degrees8 CGCONTEXTROTATECTM (CTX, m_pi_4);9     Ten     //Drawing One     //Draw Quadrilateral ACgcontextaddrect (CTX, CGRectMake ( Max, -, -, -)); -     //Draw a circle -Cgcontextaddellipseinrect (CTX, CGRectMake ( $, $, -, -)); the     //Rendering - Cgcontextstrokepath (CTX); -}
View Code

Effect:

2. Additional Notes on rotationtip: When rotating, the entire layer is rotated. Three, zoomMethod: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):
1- (void) DrawRect: (cgrect) Rect2 {3     //Get the graphics context4Cgcontextref ctx=Uigraphicsgetcurrentcontext ();5     //Matrix Operations6     //Note: The set matrix operation must be preceded by the addition of the drawing information.7     //Scaling , X-direction scaling 0.5 times times, y-direction scaling 1.5 times times8CGCONTEXTSCALECTM (CTX,0.5,1.5);9     Ten     //Drawing One     //Draw Quadrilateral ACgcontextaddrect (CTX, CGRectMake ( Max, -, -, -)); -     //Draw a circle -Cgcontextaddellipseinrect (CTX, CGRectMake ( $, $, -, -)); the     //Rendering - Cgcontextstrokepath (CTX); -}
View Code

Effect:

four, translationMethod:CGCONTEXTTRANSLATECTM (< #CGContextRef C#>, < #CGFloat Tx#>, < #CGFloat ty#>)The method receives three parameters (graph context, offset in x direction, offset in y direction) code example:
1- (void) DrawRect: (cgrect) Rect2 {3     //Get the graphics context4Cgcontextref ctx=Uigraphicsgetcurrentcontext ();5     //Matrix Operations6     //Note: The set matrix operation must be preceded by the addition of the drawing information.7     //Shift , X-direction move 50,y direction8CGCONTEXTTRANSLATECTM (CTX, -, -);9     Ten     //Drawing One     //Draw Quadrilateral ACgcontextaddrect (CTX, CGRectMake ( Max, -, -, -)); -     //Draw a circle -Cgcontextaddellipseinrect (CTX, CGRectMake ( $, $, -, -)); the     //Rendering - Cgcontextstrokepath (CTX); -}
View Code

Effect:

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

IOS quartz2d 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.