IOS4 note 16 (6) Graphics Context State

Graphics Context State

When you draw in a graphics context, the drawing obeys the context’s current settings. Thus, the way you draw using Core Graphics functions is to configure the context’s settings first, and then draw. For example, to draw a red line followed by a blue line, you would first set the context’s  line color to red, and then draw the first  line; then you’d set the context’s line color to blue, and then draw the second line. To the eye, it appears that the redness and blueness are properties of the individual lines, but in fact, at the time you draw each of them, they are properties of the entire graphics context. 

A graphics context thus has, at every moment, a state, which is the sum total of all its settings; the way a piece of drawing looks is the result of what the graphics context’s state was at the moment that piece of drawing was performed. To help you manipulate entire states, the graphics context provides a stack for holding states. Every time you call CGContextSaveGState, the context pushes the entire current state onto the stack; every time you call CGContextRestoreGState, the context retrieves the state from the top of the stack (the state that was most recently pushed) and sets itself to that state. Many of the settings that constitute a graphics context’s state, and that determine the behavior and appearance of drawing performed at that moment, are similar to those of any drawing application. They include (along with some of the commands that determine them):

Line thickness and dash style

CGContextSetLineWidth, CGContextSetLineDash

Line end-cap style and join style

CGContextSetLineCap, CGContextSetLineJoin, CGContextSetMiterLimit

Line color or pattern

CGContextSetRGBStrokeColor, CGContextSetGrayStrokeColor, CGContextSetStrokeColorWithColor, CGContextSetStrokePattern

Fill color or pattern

CGContextSetRGBFillColor,  CGContextSetGrayFillColor,  

CGContextSetFillColorWithColor, CGContextSetFillPattern

Shadow

CGContextSetShadow, CGContextSetShadowWithColor

Blend mode

CGContextSetBlendMode (this determines how drawing that you do now will be composited with drawing already present)

Overall transparency

CGContextSetAlpha (individual colors also have an alpha component)

Text features

CGContextSelectFont, CGContextSetFont, CGContextSetFontSize, CGContextSetTextDrawingMode, CGContextSetCharacterSpacing

Whether anti-aliasing and font smoothing are in effect

CGContextSetShouldAntialias, CGContextSetShouldSmoothFonts

 

Additional settings include:

Clipping area

Drawing outside the clipping area is not physically drawn.

Transform (or “CTM,” for “current transform matrix”)

Changes how points that you specify in subsequent drawing commands are mapped onto

the physical space of the canvas.

 

你可能感兴趣的:(IOS4 note 16 (6) Graphics Context State)