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 operation
1. Draw a quadrilateralA four-sided drawing is done by setting two endpoints (length and width). Code:View CodeDescription: Drawing rectangles In this way has weaknesses: the drawn rectangles are always 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#>) This accepts two parameters (graphics context, radians) Note: Setting the matrix operation must be done before adding a graphic, if it is finished after adding a graphic, it is not valid. Code:View CodeEffect: Two, about rotation 1. Rotate the demo view to display the view because it has a layer on 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):View CodeEffect: Code 2 (rotation):View Code
Effect:
2. Additional instructions on rotation tip: When rotating, the entire layer is rotated. Three, scaling method: Cgcontextscalectm (< #CGContextRef C#>, < #CGFloat Sx#>, < #CGFloat sy#>) The method receives three parameters (the graphics context, Scaling in the x direction, example of scaling code in the y direction:View Code
Effect:
Four, translation method: Cgcontexttranslatectm (< #CGContextRef C#>, < #CGFloat Tx#>, < #CGFloat ty#>) The method receives three parameters (the graphics context, X-direction offset, offset in Y-direction) code example:View Code
Effect:
Tip: The coordinate origin is the upper-left corner of the view.
iOS Development UI Chapter-quartz2d use (Matrix operations)