iPhone繪圖關於QuartZ中繪製Polygons案例

來源:互聯網
上載者:User

iPhone繪圖關於QuartZ繪製Polygons案例是本文要介紹的內容,主要介紹了如何在QuartZ繪製Polygons的內容,內容不多,主要是基於代碼實現,一起來看這個有趣的案例。

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); 

此兩句繪製的是左上方的矩形,當CGContextStrokePath調用之後,current path會被清空。

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

上面的一條語句等價於上面的兩條。

語句

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

等價與上面的語句在加上CGContextSetLineWidth(10.0)

下面的三條語句通過兩種方法來fill矩形地區。

 
  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)); 

結果如:

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); 

我們會根據drawingMode的五個常量討論

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

1)kCGPathFill如:

此fill 模式為預設模式非零纏繞數原則),大概規則為,在需要填充顏色的地區的一點向畫地區外畫一條線,g如果是從左向右穿過的,則加1,如果從右向左穿過,則減一,最後結果為0則不fill,大於0則填充,所以line的方向對fill的地區有影響。

還有一種為even-odd奇偶原則),只計算line穿過path段的個數,為偶數時,不填充,奇數時填充,所以path的方向不會影響填充的結果。

2) kCGPathEOFill模式

此填充模式為奇偶模式

3)kCGPathStroke模式

4)kCGPathFillStroke模式

5)kCGPathEOFillStroke模式

小結:iPhone繪圖關於QuartZ繪製Polygons案例的內容介紹完了,希望本文對你有所協助!如果想深入瞭解iphone繪圖的更多內容,請參考:

iPhone繪圖關於QuartZ中繪製Line案例

iPhone繪圖關於QuartZ中繪製Curves案例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.