CoreGraphics 学习摘记之 Paths

Points

You can call the function  CGContextMoveToPoint  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 function  CGContextAddCurveToPoint  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 function  CGContextAddEllipseInRect

Rectangles

You can add a rectangle to the current path by calling the function  CGContextAddRect 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.

Quartz provides two data types for creating reusable paths—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. 
Table 3-1  Parameters that affect how Quartz strokes the current path

Parameter

Function to set parameter value

Line width

CGContextSetLineWidth

Line join

CGContextSetLineJoin

Line cap

CGContextSetLineCap

Miter limit

CGContextSetMiterLimit

Line dash pattern

CGContextSetLineDash

Stroke color space

CGContextSetStrokeColorSpace

Stroke color

CGContextSetStrokeColorCGContextSetStrokeColorWithColor

Stroke pattern

CGContextSetStrokePattern

Functions for Stroking a Path

Table 3-4  Functions that stroke paths

Function

Description

CGContextStrokePath

Strokes the current path.

CGContextStrokeRect

Strokes the specified rectangle.

CGContextStrokeRectWithWidth

Strokes the specified rectangle, using the specified line width.

CGContextStrokeEllipseInRect

Strokes an ellipse that fits inside the specified rectangle.

CGContextStrokeLineSegments

Strokes a sequence of lines.

CGContextDrawPath

If you pass the constant kCGPathStroke, strokes the current path. See “Filling a Path” if you want to both fill and stroke a path.

Filling a Path

The default fill rule is called the nonzero winding number rule

Another rule is even-odd rule.
Table 3-5  Functions that fill paths

Function

Description

CGContextEOFillPath

Fills the current path using the even-odd rule.

CGContextFillPath

Fills the current path using the nonzero winding number rule.

CGContextFillRect

Fills the area that fits inside the specified rectangle.

CGContextFillRects

Fills the areas that fits inside the specified rectangles.

CGContextFillEllipseInRect

Fills an ellipse that fits inside the specified rectangle.

CGContextDrawPath

Fills the current path if you pass kCGPathFill (nonzero winding number rule) or kCGPathEOFill (even-odd rule). Fills and strokes the current path if you pass kCGPathFillStroke or kCGPathEOFillStroke.

Setting Blend Modes

You can set the blend mode to achieve a variety of effects by calling the function  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.

Table 3-6  Functions that clip the graphics context

Function

Description

CGContextClip

Uses the nonzero winding number rule to calculate the intersection of the current path with the current clipping path.

CGContextEOClip

Uses the even-odd rule to calculate the intersection of the current path with the current clipping path.

CGContextClipToRect

Sets the clipping area to the area that intersects both the current clipping path and the specified rectangle.

CGContextClipToRects

Sets the clipping area to the area that intersects both the current clipping path and region within the specified rectangles.

CGContextClipToMask

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.”)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值