Points
You can call the functionCGContextMoveToPoint
to specify a starting position for a new subpath. I
f you call the function
CGContextMoveToPoint
to set a location at (10,10), that moves the current point to (10,10). If you then draw a horizontal line 50 units long, the last point on the line, that is, (60,10), becomes the current point.
Lines
Its starting point is always assumed to be the current point, so when you create a line, you specify only its endpoint. You use the functionCGContextAddLineToPoint
to append a single line to a subpath.
You can add a series of connected lines to a path by calling the function CGContextAddLines
. You pass this function an array of points. The first point must be the starting point of the first line; the remaining points are endpoints.
Arcs
Arcs are circle segments. Quartz provides two functions that create arcs.
The function CGContextAddArc
creates a curved segment from a circle. You specify the center of the circle, the radius, and the radial angle (in radians).
The function CGContextAddArcToPoint
is ideal to use when you want to round the corners of a rectangle.
Curves
You use the functionCGContextAddCurveToPoint
to append a cubic curve from the current point, using control points and an endpoint you specify. The placement of the two control points determines the geometry of the curve. If the second control point is closer to the current point (starting point) than the first control point, the curve crosses over itself.
You can append a quadratic curve from the current point by calling the function CGContextAddQuadCurveToPoint
, and specifying a control point and an endpoint.
Closing a Subpath
To close the current subpath, your application should call CGContextClosePath
.
Ellipses
You can add an ellipse to the current path by calling the functionCGContextAddEllipseInRect
.
Rectangles
You can add a rectangle to the current path by calling the functionCGContextAddRect
.
You can add many rectangles to the current path by calling the function
CGContextAddRects。
Creating a Path
-
Before you begin a new path, call the function
CGContextBeginPath
. -
Lines, arcs, and curves are drawn starting at the current point. An empty path has no current point; you must call
CGContextMoveToPoint
to set the starting point for the first subpath or call a convenience function that implicitly does this for you. -
When you want to close the current subpath within a path, call the function
CGContextClosePath
to connect a segment to the starting point of the subpath.
-
When you draw arcs, Quartz draws a line between the current point and the starting point of the arc.
-
Quartz routines that add ellipses and rectangles add a new closed subpath to the path.
-
You must call a painting function to fill or stroke the path because creating a path does not draw the path.
CGPathRef
and CGMutablePathRef
. You can call the function CGPathCreateMutable
to create a mutable CGPath object to which you can add lines, arcs, curves, and rectangles. When you want to append the path to a graphics context, you call the function CGContextAddPath
.
Painting a Path
You can paint the current path by stroking or filling or both. Stroking paints a line that straddles the path. Filling paints the area contained within the path. Parameter | Function to set parameter value |
---|---|
Line width | |
Line join | |
Line cap | |
Miter limit | |
Line dash pattern | |
Stroke color space | |
Stroke color | |
Stroke pattern |
Functions for Stroking a Path
Function | Description |
---|---|
| Strokes the current path. |
| Strokes the specified rectangle. |
| Strokes the specified rectangle, using the specified line width. |
| Strokes an ellipse that fits inside the specified rectangle. |
| Strokes a sequence of lines. |
| If you pass the constant |
Filling a Path
The default fill rule is called the nonzero winding number rule.
Function | Description |
---|---|
| Fills the current path using the even-odd rule. |
| Fills the current path using the nonzero winding number rule. |
| Fills the area that fits inside the specified rectangle. |
| Fills the areas that fits inside the specified rectangles. |
| Fills an ellipse that fits inside the specified rectangle. |
| Fills the current path if you pass |
Setting Blend Modes
CGContextSetBlendMode
, passing the appropriate blend mode constant. Keep in mind that the blend mode is part of the graphics state. If you use the function
CGContextSaveGState
prior to changing the blend mode, then calling the function
CGContextRestoreGState
resets the blend mode to normal.
Clipping to a Path
When you paint, Quartz renders paint only within the clipping area. Drawing that occurs inside the closed subpaths of the clipping area is visible; drawing that occurs outside the closed subpaths of the clipping area is not. The clipping area is part of the graphics state. Function | Description |
---|---|
Uses the nonzero winding number rule to calculate the intersection of the current path with the current clipping path. | |
Uses the even-odd rule to calculate the intersection of the current path with the current clipping path. | |
Sets the clipping area to the area that intersects both the current clipping path and the specified rectangle. | |
Sets the clipping area to the area that intersects both the current clipping path and region within the specified rectangles. | |
Maps a mask into the specified rectangle and intersects it with the current clipping area of the graphics context. Any subsequent path drawing you perform to the graphics context is clipped. (See “Masking an Image by Clipping the Context.”) |