iPhone繪圖關(guān)于QuartZ中繪制Polygons案例
iPhone繪圖關(guān)于QuartZ中繪制Polygons案例是本文要介紹的內(nèi)容,主要介紹了如何在QuartZ中繪制Polygons的內(nèi)容,內(nèi)容不多,主要是基于代碼實(shí)現(xiàn),一起來(lái)看這個(gè)有趣的案例。
1.繪制矩形的一般方法
- // Drawing with a white stroke color
- CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
- // And drawing with a blue fill color
- CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
- // Draw them with a 2.0 stroke width so they are a bit more visible.
- CGContextSetLineWidth(context, 2.0);
- // Add Rect to the current path, then stroke it
- CGContextAddRect(context, CGRectMake(30.0, 30.0, 60.0, 60.0));
- CGContextStrokePath(context);
- // Stroke Rect convenience that is equivalent to above
- CGContextStrokeRect(context, CGRectMake(30.0, 120.0, 60.0, 60.0));
- // Stroke rect convenience equivalent to the above, plus a call to CGContextSetLineWidth().
- CGContextStrokeRectWithWidth(context, CGRectMake(30.0, 210.0, 60.0, 60.0), 10.0);
- // Demonstate the stroke is on both sides of the path.
- CGContextSaveGState(context);
- //red
- CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
- CGContextStrokeRectWithWidth(context, CGRectMake(30.0, 210.0, 60.0, 60.0), 2.0);
- CGContextRestoreGState(context);
- CGRect rects[] =
- {
- CGRectMake(120.0, 30.0, 60.0, 60.0),
- CGRectMake(120.0, 120.0, 60.0, 60.0),
- CGRectMake(120.0, 210.0, 60.0, 60.0),
- };
- // Bulk call to add rects to the current path.
- CGContextAddRects(context, rects, sizeof(rects)/sizeof(rects[0]));
- CGContextStrokePath(context);
- // Create filled rectangles via two different paths.
- // Add/Fill path
- CGContextAddRect(context, CGRectMake(210.0, 30.0, 60.0, 60.0));
- CGContextFillPath(context);
- // Fill convienience.
- CGContextFillRect(context, CGRectMake(210.0, 120.0, 60.0, 60.0));
注釋:
- CGContextAddRect(context, CGRectMake(30.0, 30.0, 60.0, 60.0));
- CGContextStrokePath(context);
此兩句繪制的是左上角的矩形,當(dāng)CGContextStrokePath調(diào)用之后,current path會(huì)被清空。
- CGContextStrokeRect(context, CGRectMake(30.0, 120.0, 60.0, 60.0));
上面的一條語(yǔ)句等價(jià)于上面的兩條。
語(yǔ)句
- CGContextStrokeRectWithWidth(context, CGRectMake(30.0, 210.0, 60.0, 60.0), 10.0)
等價(jià)與上面的語(yǔ)句在加上CGContextSetLineWidth(10.0)
下面的三條語(yǔ)句通過(guò)兩種方法來(lái)fill矩形區(qū)域。
- CGContextAddRect(context, CGRectMake(210.0, 30.0, 60.0, 60.0));
- CGContextFillPath(context);
- // Fill convienience.
- CGContextFillRect(context, CGRectMake(210.0, 120.0, 60.0, 60.0));
結(jié)果如下圖:
2.繪制多邊形(Polygon)
- // Drawing with a white stroke color
- CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
- // Drawing with a blue fill color
- CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
- // Draw them with a 2.0 stroke width so they are a bit more visible.
- CGContextSetLineWidth(context, 2.0);
- CGPoint center;
- // Add a star to the current path
- center = CGPointMake(90.0, 90.0);
- CGContextMoveToPoint(context, center.x, center.y + 60.0);
- for(int i = 1; i < 5; ++i)
- {
- CGFloat x = 60.0 * sinf(i * 4.0 * M_PI / 5.0);
- CGFloat y = 60.0 * cosf(i * 4.0 * M_PI / 5.0);
- CGContextAddLineToPoint(context, center.x + x, center.y + y);
- }
- // And close the subpath.
- CGContextClosePath(context);
- // Now add the hexagon to the current path
- center = CGPointMake(210.0, 90.0);
- CGContextMoveToPoint(context, center.x, center.y + 60.0);
- for(int i = 1; i < 6; ++i)
- {
- CGFloat x = 60.0 * sinf(i * 2.0 * M_PI / 6.0);
- CGFloat y = 60.0 * cosf(i * 2.0 * M_PI / 6.0);
- CGContextAddLineToPoint(context, center.x + x, center.y + y);
- }
- // And close the subpath.
- CGContextClosePath(context);
- // Now draw the star & hexagon with the current drawing mode.
- CGContextDrawPath(context, drawingMode);
我們會(huì)根據(jù)drawingMode的五個(gè)常量討論
- kCGPathFill, kCGPathEOFill, kCGPathStroke, kCGPathFillStroke, or kCGPathEOFillStroke.
(1)kCGPathFill如下圖:
此fill 模式為缺省模式(非零纏繞數(shù)原則),大概規(guī)則為,在需要填充顏色的區(qū)域的一點(diǎn)向畫(huà)區(qū)域外畫(huà)一條線,g如果是從左向右穿過(guò)的,則加1,如果從右向左穿過(guò),則減一,最后結(jié)果為0則不fill,大于0則填充,所以line的方向?qū)ill的區(qū)域有影響。
還有一種為even-odd(奇偶原則),只計(jì)算line穿過(guò)path段的個(gè)數(shù),為偶數(shù)時(shí),不填充,奇數(shù)時(shí)填充,所以path的方向不會(huì)影響填充的結(jié)果。
(2) kCGPathEOFill模式
此填充模式為奇偶模式
(3)kCGPathStroke模式
(4)kCGPathFillStroke模式
(5)kCGPathEOFillStroke模式
小結(jié):iPhone繪圖關(guān)于QuartZ中繪制Polygons案例的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!如果想深入了解iphone繪圖的更多內(nèi)容,請(qǐng)參考: