For the students just contact QUARTZ2D, first understand the concept of the context, and then from the most basic line to experience the quartz2d of the drawing steps
Introduction QUART2D: Apple's official two-dimensional (planar) drawing engine, while supporting both iOS and MacOS systems, its API is pure C language, it can draw graphics, draw text, draw pictures, custom UI controls
In iOS development, quartz2d is most commonly used to customize UI controls.
quartz2d Drawing steps: 1: Getting the Graphics Context object 2 Adding a path to the graphics context object, drawing properties, and so on 3 rendering (drawing the path in the graphics context to the corresponding output device)
Describes the graphics context: is a cgcontextref type of data. For beginners, do not have a lot of pressure, White is when we were a child when drawing, the first to have grass manuscript, in the draft paper first described, and then copied to the artboard, the context is actually like grass manuscript.
The graphics context mainly contains the following information: 1 drawing paths (various graphs: lines, triangles, circles ...) 2 drawing status: (Color, line width, style, rotation ...) 3 output target: (where to draw?) UIView, pictures, pdf ...) So, with the same drawing sequence, making different contexts, you can draw the graph to different targets. If the concept is not clear to everyone, then experience the context in the code.
Here are 5 kinds of drawing methods, we slowly realize
First do some preparatory work:
Next, open the custom class HFVIEW.M file, in-(void) DrawRect: (cgrect) rect;
Why draw in this method. 1 because the graphics context can only be obtained in this method, the context cannot be obtained in another method,
and-(void) DrawRect: (cgrect) Rect, cannot be called in another method, because only the system itself calls this method to get the context, its own manual call is not to get the context of the
Summary: The drawing code is written in-(void) DrawRect: (CGRect) The Rect method, because: 1 only in this method can get to the context, 2 this method only when the system calls, can get the context, itself in other methods manually called, is not available in the context.
Below, start writing code:
The first: pure C language, directly add the path to the context
Second: pure C language, first create a C path, and then add to the context
Third: C language, nested OC path
Fourth: C language C path nested OC path
Fifth Pure OC [everyone can experience the simplicity of object-oriented]
The above for you to introduce the 5 drawing method, after drawing pictures, drawing text, with more is the C path OC path used together, because there are some features, pure OC is impossible to achieve, so we focus on practicing the use of a third way.
The following describes the drawing property settings:
First introduce the C Path property setting
Line properties are set up in the above categories, interested students can try, what each effect looks like.
The following describes the OC Path property settings
We said at the beginning that in addition to saving paths in context, there are drawing properties and output targets, what will the context stack do when the rendering method executes?
Let's take a look at the principle of the context stack
Write code to verify the reasoning above
It proves that our reasoning is correct, and the principle of context stacks is the same.
The following is to introduce common graphics drawing methods, each kind of graphics in a way to draw, interested students can try to use another method of drawing, proficiency in several drawing methods, so that everyone deeper understanding of quartz2d drawing steps
Drawing triangles: plain C, adding paths directly to the context
Can also be written like this
Right angle rectangular pure OC mode
Rounded rectangles,
Because the OC path has a direct class creation method, it is more convenient to use, so the OC
Round, there are many ways to modify the rounded rectangle data.
Drawing of Arcs
Fan-shaped drawing adds two lines on the basis of an arc
We introduce the simple use of quartz2d and the drawing of simple graphs. Hope to be helpful to everyone
IOS quartz2d Drawing