自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone繪圖關(guān)于QuartZ中繪制Polygons案例

移動(dòng)開(kāi)發(fā) iOS
iPhone繪圖關(guān)于QuartZ中繪制Polygons案例是本文要介紹的內(nèi)容,主要介紹了如何在QuartZ中繪制Polygons的內(nèi)容,內(nèi)容不多,主要是基于代碼實(shí)現(xiàn),一起來(lái)看這個(gè)有趣的案例。

iPhone繪圖關(guān)于QuartZ繪制Polygons案例是本文要介紹的內(nèi)容,主要介紹了如何在QuartZ繪制Polygons的內(nèi)容,內(nèi)容不多,主要是基于代碼實(shí)現(xiàn),一起來(lái)看這個(gè)有趣的案例。

1.繪制矩形的一般方法

  1. // Drawing with a white stroke color  
  2. CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);  
  3. // And drawing with a blue fill color  
  4. CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);  
  5. // Draw them with a 2.0 stroke width so they are a bit more visible.  
  6. CGContextSetLineWidth(context, 2.0);  
  7. // Add Rect to the current path, then stroke it  
  8. CGContextAddRect(context, CGRectMake(30.0, 30.0, 60.0, 60.0));  
  9. CGContextStrokePath(context);  
  10. // Stroke Rect convenience that is equivalent to above  
  11. CGContextStrokeRect(context, CGRectMake(30.0, 120.0, 60.0, 60.0));  
  12. // Stroke rect convenience equivalent to the above, plus a call to CGContextSetLineWidth().  
  13. CGContextStrokeRectWithWidth(context, CGRectMake(30.0, 210.0, 60.0, 60.0), 10.0);  
  14. // Demonstate the stroke is on both sides of the path.  
  15. CGContextSaveGState(context);  
  16. //red  
  17. CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);  
  18. CGContextStrokeRectWithWidth(context, CGRectMake(30.0, 210.0, 60.0, 60.0), 2.0);  
  19. CGContextRestoreGState(context);  
  20. CGRect rects[] =   
  21. {  
  22. CGRectMake(120.0, 30.0, 60.0, 60.0),  
  23. CGRectMake(120.0, 120.0, 60.0, 60.0),  
  24. CGRectMake(120.0, 210.0, 60.0, 60.0),  
  25. };  
  26. // Bulk call to add rects to the current path.  
  27. CGContextAddRects(context, rects, sizeof(rects)/sizeof(rects[0]));  
  28. CGContextStrokePath(context);  
  29. // Create filled rectangles via two different paths.  
  30. // Add/Fill path  
  31. CGContextAddRect(context, CGRectMake(210.0, 30.0, 60.0, 60.0));  
  32. CGContextFillPath(context);  
  33. // Fill convienience.  
  34. CGContextFillRect(context, CGRectMake(210.0, 120.0, 60.0, 60.0)); 

注釋:

  1. CGContextAddRect(context, CGRectMake(30.0, 30.0, 60.0, 60.0));  
  2. CGContextStrokePath(context); 

此兩句繪制的是左上角的矩形,當(dāng)CGContextStrokePath調(diào)用之后,current path會(huì)被清空。

  1. CGContextStrokeRect(context, CGRectMake(30.0, 120.0, 60.0, 60.0)); 

上面的一條語(yǔ)句等價(jià)于上面的兩條。

語(yǔ)句

  1. CGContextStrokeRectWithWidth(context, CGRectMake(30.0, 210.0, 60.0, 60.0), 10.0) 

等價(jià)與上面的語(yǔ)句在加上CGContextSetLineWidth(10.0)

下面的三條語(yǔ)句通過(guò)兩種方法來(lái)fill矩形區(qū)域。

  1. CGContextAddRect(context, CGRectMake(210.0, 30.0, 60.0, 60.0));  
  2. CGContextFillPath(context);  
  3. // Fill convienience.  
  4. CGContextFillRect(context, CGRectMake(210.0, 120.0, 60.0, 60.0)); 

結(jié)果如下圖:

iPhone繪圖關(guān)于QuartZ中繪制Polygons案例

2.繪制多邊形(Polygon)

  1. // Drawing with a white stroke color  
  2. CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);  
  3. // Drawing with a blue fill color  
  4. CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);  
  5. // Draw them with a 2.0 stroke width so they are a bit more visible.  
  6. CGContextSetLineWidth(context, 2.0);  
  7. CGPoint center;  
  8. // Add a star to the current path  
  9. center = CGPointMake(90.0, 90.0);  
  10. CGContextMoveToPoint(context, center.x, center.y + 60.0);  
  11. for(int i = 1; i < 5; ++i)  
  12. {  
  13. CGFloat x = 60.0 * sinf(i * 4.0 * M_PI / 5.0);  
  14. CGFloat y = 60.0 * cosf(i * 4.0 * M_PI / 5.0);  
  15. CGContextAddLineToPoint(context, center.x + x, center.y + y);  
  16. }  
  17. // And close the subpath.  
  18. CGContextClosePath(context);  
  19. // Now add the hexagon to the current path  
  20. center = CGPointMake(210.0, 90.0);  
  21. CGContextMoveToPoint(context, center.x, center.y + 60.0);  
  22. for(int i = 1; i < 6; ++i)  
  23. {  
  24. CGFloat x = 60.0 * sinf(i * 2.0 * M_PI / 6.0);  
  25. CGFloat y = 60.0 * cosf(i * 2.0 * M_PI / 6.0);  
  26. CGContextAddLineToPoint(context, center.x + x, center.y + y);  
  27. }  
  28. // And close the subpath.  
  29. CGContextClosePath(context);  
  30. // Now draw the star & hexagon with the current drawing mode.  
  31. CGContextDrawPath(context, drawingMode); 

我們會(huì)根據(jù)drawingMode的五個(gè)常量討論

  1. kCGPathFill, kCGPathEOFill, kCGPathStroke, kCGPathFillStroke, or kCGPathEOFillStroke. 

(1)kCGPathFill如下圖:

iPhone繪圖關(guān)于QuartZ中繪制Polygons案例

此fill 模式為缺省模式(非零纏繞數(shù)原則),大概規(guī)則為,在需要填充顏色的區(qū)域的一點(diǎn)向畫(huà)區(qū)域外畫(huà)一條線,g如果是從左向右穿過(guò)的,則加1,如果從右向左穿過(guò),則減一,最后結(jié)果為0則不fill,大于0則填充,所以line的方向?qū)ill的區(qū)域有影響。

iPhone繪圖關(guān)于QuartZ中繪制Polygons案例

還有一種為even-odd(奇偶原則),只計(jì)算line穿過(guò)path段的個(gè)數(shù),為偶數(shù)時(shí),不填充,奇數(shù)時(shí)填充,所以path的方向不會(huì)影響填充的結(jié)果。

(2) kCGPathEOFill模式

iPhone繪圖關(guān)于QuartZ中繪制Polygons案例

此填充模式為奇偶模式

(3)kCGPathStroke模式

iPhone繪圖關(guān)于QuartZ中繪制Polygons案例

(4)kCGPathFillStroke模式

iPhone繪圖關(guān)于QuartZ中繪制Polygons案例

(5)kCGPathEOFillStroke模式

iPhone繪圖關(guān)于QuartZ中繪制Polygons案例

小結(jié):iPhone繪圖關(guān)于QuartZ繪制Polygons案例的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!如果想深入了解iphone繪圖的更多內(nèi)容,請(qǐng)參考:

iPhone繪圖關(guān)于QuartZ中繪制Line案例

iPhone繪圖關(guān)于QuartZ中繪制Curves案例

責(zé)任編輯:zhaolei 來(lái)源: 新浪微博
相關(guān)推薦

2011-08-12 11:08:45

iPhone繪圖QuartZ繪制

2011-08-12 10:46:18

iPhone繪圖繪制QuartZ

2011-08-17 14:32:44

iOS開(kāi)發(fā)繪制

2011-08-09 14:54:29

iPhoneNSDateanotherDate

2011-08-18 15:24:40

iPhone國(guó)際化

2011-08-10 18:24:22

iPhone 圖形 繪圖

2011-08-19 10:05:30

iPhone開(kāi)發(fā)

2011-08-15 15:44:46

iPhone開(kāi)發(fā)PDF

2011-08-18 16:24:44

iPhone開(kāi)發(fā)圖片

2011-08-17 14:27:17

Core AnimatQuartz2D

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-07-29 13:27:48

iPhone 開(kāi)發(fā) Nib

2011-08-16 15:48:37

iPhone開(kāi)發(fā)抓圖程序

2011-08-22 14:21:24

iPhone開(kāi)發(fā)UIView Anim

2014-04-29 14:27:59

OpenGL ES 2Android繪制紋理

2011-08-15 13:44:07

iPhone開(kāi)發(fā)UITableView

2011-08-22 15:15:49

iPhone開(kāi)發(fā)NSMutableAr排序

2011-08-08 14:07:49

iPhone開(kāi)發(fā) 字體

2011-08-15 09:58:25

iPhoneXib文件UITableView

2011-08-16 18:56:11

iPhone開(kāi)發(fā)Three20
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)