Quartz 2D指南3---paths

1.path的创建和path的painting

下图的圆形区域就是一个clipping area。

2. the building blocks
subpaths是由lines,arcs,curves组建成的,Quartz也提供了一些方便的函数用于添加矩形和椭圆。
(1)points
我们可以调用函数CGContextMoveToPoint去指定一个新的subpath的开始位置。Quratz会保存当前点的记录。例如,如果我们调用函数CGContextMoveToPoint去设置位置(10,10),则current point就为(10,10)。如果我们接着在水平方向绘制50像素,则line的最后点为(60,10),此点也变成了current point。绘制line,arcs,curve都是从current point开始的。
(2)Lines
一个line定义了他的endpoint,line的开始点为current point。所以当我们创建一个line时,我们只用指定他的endpoint。我们用函数CGContextAddLineToPoint去在subpath上添加一条线。
(3)Arcs
Arcs是圆的一部分。Quratz提供了两个函数用来创建arcs。函数CGContextAddArc用来从一个圆中创建一个曲线段。
函数CGContextAddArcToPoint是一个理想的方法用来圆一个矩形的角。Quratz使用我们提供的endpoint去创建两个切线。

(4)curves
Quadratic 和Bezier 曲线可以指定很多形状的曲线。应用多项式去计算曲线上的点,需要用到起点,终点,以及一个或者多个control 点。


(5)closing a subpath
(6)ellipses
(7)Rectangles
3.创建path。
当我们在graphics context下创建一个path,可应该先调用Quartz的函数CGContextBeginPath,接着设置第一个shape或者subpath的开始点,在path中调用函数CGContextMoveToPoint。当初始点设置好以后,就可以添加lines,arcs,curves,但要注意一下几点:
(1)在开始新的path之前,要调用函数CGContextBeginPath。
(2)lines,arcs,curves都是从current point开始绘制,一个空的path没有current point,我们必须调用CGContextMoveToPoint去设置或者调用一个函数隐含的设置了当前值。
(3)调用函数CGContextClosePath去关闭current subpath。接下来会开始一个新的subpath,即时我们不去那个设置开始点。
(4)当我们绘制一个arcs时,Quratz会在current point 和start point之间绘制一条直线。
(5)创建一个path不等于绘制path,所以我们必须调用paint 函数去fill 或者stroke path。
4.Paint path
影响stroking的参数
下表就是这些参数,这些参数是graphics state的一部分
Parameter | Function to set parameter value |
|---|---|
Line width | |
Line join | |
Line cap | |
Miter limit | |
Line dash pattern | |
Stroke color space | |
Stroke color | |
Stroke pattern |
stroking 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 |
函数CGContextStrokeLineSegments 等同于下面的代码:
CGContextBeginPath (context); |
for (k = 0; k < count; k += 2) {
|
CGContextMoveToPoint(context, s[k].x, s[k].y); |
CGContextAddLineToPoint(context, s[k+1].x, s[k+1].y); |
} |
CGContextStrokePath(context); |
浙公网安备 33010602011771号