"iOS Dev-80" quartz2d Drawing Introduction: Line/Circle/ellipse/square and context stack management Cgcontextsavegstate/cgcontextrestoregstate

Source: Internet
Author: User
Tags vars

This article reprinted to http://blog.csdn.net/weisubao/article/details/41282457

[OBJC]View Plaincopy
  1. -(void) DrawRect: (cgrect) rect {
  2. //Get current context
  3. Cgcontextref Ctx=uigraphicsgetcurrentcontext ();
  4. //Save the current context state in the stack
  5. Cgcontextsavegstate (CTX);
  6. //Zoom, move processing (need to be set before drawing)
  7. CGCONTEXTSCALECTM (CTX, 0. 5, 0. 5);
  8. CGCONTEXTTRANSLATECTM (CTX, 100, 100);
  9. CGCONTEXTROTATECTM (CTX, M_pi_4);
  10. //Stroke
  11. Cgcontextmovetopoint (CTX, 10, 10);
  12. Cgcontextaddlinetopoint (CTX, 100, 100);
  13. Cgcontextaddlinetopoint (CTX, 150, 50);
  14. two ways to close the loop
  15. //cgcontextaddlinetopoint (CTX, 10, 10);
  16. Cgcontextclosepath (CTX);
  17. //Render drawing, solid and hollow
  18. Cgcontextstrokepath (CTX);
  19. //cgcontextfillpath (CTX);
  20. //Save the current context state in the stack
  21. Cgcontextsavegstate (CTX);
  22. //Draw a square
  23. Cgcontextaddrect (CTX, CGRectMake (100, 100, 50, 50));
  24. //Set the line width (must be before Cgcontextstrokepath)
  25. //Because there was a previous rendering of the drawing, so this property setting does not affect the above triangle, the following color settings similarly
  26. ///So, if you want to set the properties of two or more shapes separately, render the drawing one at a time
  27. Cgcontextsetlinewidth (CTX, 10);
  28. //Set color (in the same vein, the code for the property setting is preceded by the drawing's code)
  29. Cgcontextsetrgbstrokecolor (CTX, 1, 0, 0, 1);
  30. Cgcontextstrokepath (CTX);
  31. //Set style
  32. Cgcontextmovetopoint (CTX, 20, 160);
  33. Cgcontextaddlinetopoint (CTX, 200, 280);
  34. Cgcontextaddlinetopoint (CTX, 250, 200);
  35. Cgcontextsetlinewidth (CTX, 20);
  36. //Set the Kinsoku style
  37. Cgcontextsetlinecap (CTX, Kcglinecapround);
  38. //Set the corner style
  39. Cgcontextsetlinejoin (CTX, Kcglinejoinround);
  40. Cgcontextstrokepath (CTX);
  41. //Remove the context state stored in the stack and restore it. The above code setting style does not affect the other
  42. Cgcontextrestoregstate (CTX);
  43. //Draw Ellipse
  44. Cgcontextaddellipseinrect (CTX, CGRectMake (200, 130, 60, 30));
  45. //below equivalent
  46. //cgcontextstrokepath (CTX);
  47. Cgcontextdrawpath (CTX, Kcgpathstroke);
  48. //Draw round
  49. Cgcontextaddellipseinrect (CTX, CGRectMake (140, 170, 50, 50));
  50. Cgcontextsetlinewidth (CTX, 3);
  51. Cgcontextstrokepath (CTX);
  52. //Draw arc
  53. Cgcontextaddarc (CTX, 200, 50, 50, M_pi_4, M_PI, 1);
  54. Cgcontextstrokepath (CTX);
  55. //Draw 1/4 circles, and a new way to set the color
  56. Cgcontextmovetopoint (CTX, 10, 230);
  57. Cgcontextaddlinetopoint (CTX, 10, 280);
  58. Cgcontextaddlinetopoint (CTX, 60, 280);
  59. Cgcontextaddarc (CTX, 10, 280, 50, 0,-m_pi_2, 1);
  60. [[Uicolor Greencolor] Setstroke];
  61. Cgcontextstrokepath (CTX);
  62. //Draw pictures and text (do not need to get context manually)
  63. nsstring *str1=@ "Xin-chou-year-empty work";
  64. [str1 drawatpoint:cgpointzero withattributes: nil];
  65. UIImage *img=[uiimage imagenamed:@ "001"];
  66. [img drawatpoint:cgpointmake (10, 10)];
  67. //overlap pictures and sign in one box
  68. CGRect rect1=cgrectmake (50, 50, 10 0, 10 0);
  69. [img drawaspatterninrect:rect1];
  70. nsmutabledictionary *attr=[[nsmutabledictionary Alloc]init];
  71. Attr[nsforegroundcolorattributename]=[uicolor Whitecolor];
  72. Attr[nsfontattributename]=[uifont systemfontofsize:13];
  73. [str1 drawinrect:cgrectmake (50, 140, 10 0, 10 0)   WITHATTRIBUTES:ATTR];
  74. //Remove the context state stored in the stack and restore it. The above code setting style does not affect the other
  75. Cgcontextrestoregstate (CTX);
  76. //Cut round head
  77. Cgcontextaddellipseinrect (CTX, CGRectMake (150, 150, 10 0, 100))  ;
  78. //Cut out a context area by a circle, and the later content is filled in the circular context
  79. Cgcontextclip (CTX);
  80. UIImage *img1=[uiimage imagenamed:@ "Me"];
  81. [img1 drawatpoint:cgpointmake (150, 150)];
  82. }

"iOS Dev-80" quartz2d Drawing Introduction: Line/Circle/ellipse/square and context stack management Cgcontextsavegstate/cgcontextrestoregstate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.