Signature drawing example on uiview.

Source: Internet
Author: User

 

The drawing software on the iPad is good. I bought a graffiti software from a child. Here is an example.

Canvas2d. h

View plaincopy to clipboardprint?
  1. # Import <uikit/uikit. h>
  2. # Import <quartzcore/quartzcore. h>
  3. @ Interface canvas2d: uiview {
  4. Nsmutablearray * arraystrokes;
  5. Uicolor * currentcolor;
  6. }
  7. @ Property (retain) nsmutablearray * arraystrokes;
  8. @ End

# Import <uikit/uikit. h> # import <quartzcore/quartzcore. h> @ interface canvas2d: uiview {nsmutablearray * arraystrokes; uicolor * currentcolor;} @ property (retain) nsmutablearray * arraystrokes; @ end

Canvas2d. m

View plaincopy to clipboardprint?
  1. # Import "canvas2d. H"
  2. @ Implementation canvas2d
  3. @ Synthesize arraystrokes;
  4. -(ID) initwithframe :( cgrect) Frame
  5. {
  6. Self = [Super initwithframe: frame];
  7. If (Self ){
  8. // Initialization code
  9. Self. arraystrokes = [nsmutablearray array];
  10. }
  11. Return self;
  12. }
  13. -(ID) initwithcoder :( nscoder *) adecoder {
  14. If (Self = [Super initwithcoder: adecoder]) {
  15. Self. arraystrokes = [nsmutablearray array];
  16. }
  17. Return self;
  18. }
  19. // Only override drawrect: If you perform custom drawing.
  20. // An empty implementation adversely affects performance during animation.
  21. -(Void) drawrect :( cgrect) rect
  22. {
  23. // Drawing code
  24. Cgcontextref context = uigraphicsgetcurrentcontext ();
  25. Cgcontextsetlinewidth (context, 2.0 );
  26. Cgcolorspaceref colorspace = cgcolorspacecreatedevicergb ();
  27. Cgfloat components [] = {0.0, 0.0, 1.0, 1.0 };
  28. Cgcolorref color = cgcolorcreate (colorspace, components );
  29. Cgcontextsetstrokecolorwithcolor (context, color );
  30. Cgcontextmovetopoint (context, 0, 0 );
  31. Cgcontextaddlinetopoint (context, 300,400 );
  32. Cgcontextstrokepath (context );
  33. Cgcolorspacerelstrap (colorspace );
  34. Cgcolorrelease (color );
  35. If (self. arraystrokes)
  36. {
  37. Int arraynum = 0;
  38. // Each iteration draw a stroke
  39. // Line segments within a single stroke (PATH) has the same color and line width
  40. For (nsdictionary * dictstroke in self. arraystrokes)
  41. {
  42. Nsarray * arraypointsinstroke = [dictstroke objectforkey: @ "points"];
  43. Uicolor * color = [dictstroke objectforkey: @ "color"];
  44. Float size = [[dictstroke objectforkey: @ "size"] floatvalue];
  45. [Color set]; // equivalent to both setfill and setstroke
  46. // Draw the stroke, line by line, with rounded Joints
  47. Uibezierpath * pathlines = [uibezierpath bezierpath];
  48. Cgpoint pointstart = cgpointfromstring ([arraypointsinstroke objectatindex: 0]);
  49. [Pathlines movetopoint: pointstart];
  50. For (INT I = 0; I <(arraypointsinstroke. Count-1); I ++)
  51. {
  52. Cgpoint pointnext = cgpointfromstring ([arraypointsinstroke objectatindex: I + 1]);
  53. [Pathlines addlinetopoint: pointnext];
  54. }
  55. Pathlines. linewidth = size;
  56. Pathlines. linejoinstyle = kcglinejoinround;
  57. Pathlines. linecapstyle = kcglinecapround;
  58. [Pathlines stroke];
  59. Arraynum ++;
  60. }
  61. }
  62. }
  63. // Start new dictionary for each touch, with points and color
  64. -(Void) touchesbegan :( nsset *) touches withevent :( uievent *) event
  65. {
  66. Nsmutablearray * arraypointsinstroke = [nsmutablearray array];
  67. Nsmutabledictionary * dictstroke = [nsmutabledictionary dictionary];
  68. [Dictstroke setobject: arraypointsinstroke forkey: @ "points"];
  69. [Dictstroke setobject: [uicolor blackcolor] forkey: @ "color"];
  70. [Dictstroke setobject: [nsnumber numberwithfloat: 5] forkey: @ "size"];
  71. Cgpoint point = [[touches anyobject] locationinview: Self];
  72. [Arraypointsinstroke addobject: nsstringfromcgpoint (point)];
  73. [Self. arraystrokes addobject: dictstroke];
  74. }
  75. // Add each point to points Array
  76. -(Void) touchesmoved :( nsset *) touches withevent :( uievent *) event
  77. {
  78. Cgpoint point = [[touches anyobject] locationinview: Self];
  79. Cgpoint prevpoint = [[touches anyobject] previuslocationinview: Self];
  80. Nsmutablearray * arraypointsinstroke = [[self. arraystrokes lastobject] objectforkey: @ "points"];
  81. [Arraypointsinstroke addobject: nsstringfromcgpoint (point)];
  82. Cgrect recttoredraw = cgrectmake (\
  83. (Prevpoint. x> point. X )? Point. X: prevpoint. X)-5 ,\
  84. (Prevpoint. Y> point. Y )? Point. Y: prevpoint. Y)-5 ,\
  85. FABS (point. x-prevPoint.x) + 2*5 ,\
  86. FABS (point. y-prevPoint.y) + 2*5 \
  87. );
  88. [Self setneedsdisplayinrect: recttoredraw];
  89. }
  90. // Send over new trace when the touch ends
  91. -(Void) touchesended :( nsset *) touches withevent :( uievent *) event
  92. {
  93. }
  94. -(Void) dealloc
  95. {
  96. [Arraystrokes release];
  97. [Super dealloc];
  98. }
  99. @ End

# Import "canvas2d. H "@ implementation canvas2d @ synthesize arraystrokes;-(ID) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; If (Self) {// initialization codeself. arraystrokes = [nsmutablearray array];} return self;}-(ID) initwithcoder :( nscoder *) adecoder {If (Self = [Super initwithcoder: adecoder]) {self. arraystrokes = [nsmutablearray array];} return self;} // only override draw Rect: If you perform custom drawing. // an empty implementation adversely affects performance during animation. -(void) drawrect :( cgrect) rect {// drawing codecgcontextref context = uigraphicsgetcurrentcontext (); cgcontextsetlinewidth (context, 2.0); cgcolorspaceref colorspace = trim (); cgfloat components [] = {0.0, 0.0, 1.0, 1.0}; cgcolorref color = cgcolorcreate (colorspace, compone ETS); cgcontextsetstrokecolorwithcolor (context, color); cgcontextmovetopoint (context, 0, 0); cgcontextaddlinetopoint (context, 300,400); cgcontextstrokepath (context); cgcolorspacerel.pdf (colorspace ); cgcolorrelease (color); If (self. arraystrokes) {int arraynum = 0; // each iteration draw a stroke // line segments within a single stroke (PATH) has the same color and line widthfor (nsdictionary * dictstroke in Self. arraystrokes) {nsarray * arraypointsinstroke = [dictstroke objectforkey: @ "points"]; uicolor * color = [dictstroke objectforkey: @ "color"]; float size = [[dictstroke objectforkey: @ "size"] floatvalue]; [color set]; // equivalent to both setfill and setstroke // draw the stroke, line by line, with rounded jointsuibezierpath * pathlines = [uibezierpath bezierpath]; cgpoint pointstart = cgpointfromstring ([ar Raypointsinstroke objectatindex: 0]); [pathlines movetopoint: pointstart]; for (INT I = 0; I <(arraypointsinstroke. count-1); I ++) {cgpoint pointnext = cgpointfromstring ([arraypointsinstroke objectatindex: I + 1]); [pathlines addlinetopoint: pointnext];} pathlines. linewidth = size; pathlines. linejoinstyle = kcglinejoinround; pathlines. linecapstyle = kcglinecapround; [pathlines stroke]; arraynum ++ ;}}// start n EW dictionary for each touch, with points and color-(void) touchesbegan :( nsset *) touches withevent :( uievent *) event {nsmutablearray * arraypointsinstroke = [nsmutablearray array]; optional * dictstroke = [nsmutabledictionary dictionary]; [dictstroke setobject: arraypointsinstroke forkey: @ "points"]; [dictstroke setobject: [uicolor blackcolor] forkey: @ "color"]; [dictstroke setobject: [nsnumber n Umberwithfloat: 5] forkey: @ "size"]; cgpoint point = [[touches anyobject] locationinview: Self]; [arraypointsinstroke addobject: nsstringfromcgpoint (point)]; [self. arraystrokes addobject: dictstroke];} // Add each point to points array-(void) touchesmoved :( nsset *) touches withevent :( uievent *) event {cgpoint point = [[touches anyobject] locationinview: Self]; cgpoint prevpoint = [[touches anyobject] previou Slocationinview: Self]; nsmutablearray * arraypointsinstroke = [[self. arraystrokes lastobject] objectforkey: @ "points"]; [arraypointsinstroke addobject: nsstringfromcgpoint (point)]; cgrect recttoredraw = cgrectmake (\ (prevpoint. x> point. x )? Point. X: prevpoint. X)-5, \ (prevpoint. Y> point. Y )? Point. y: prevpoint. y)-5, \ FABS (point. x-prevPoint.x) + 2*5, \ FABS (point. y-prevPoint.y) + 2*5 \); [self setneedsdisplayinrect: recttoredraw];} // send over new trace when the touch ends-(void) touchesended :( nsset *) touches withevent :( uievent *) event {}-(void) dealloc {[arraystrokes release]; [Super dealloc];} @ end

Remember to add quartzcore. Framework

Usage: Associate view with canvas2d in XIB

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.