The main principles include:
Inherit UIView, Overload DrawRect, and overload touch events
The function to be implemented is also, the path array is saved and so on.
Saving path path with variable data
The drawing curve is achieved through two Bezier curves.
Here you get the UIImage object for the drawing.
Uigraphicsbeginimagecontext (self.bounds.size); [Self.layer Renderincontext:uigraphicsgetcurrentcontext ()]; UIImage *result=uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); return result;
#import "Testdrow.h" @implementation testdrow uicolor *pick_color;int Choose; Uibezierpath *mpath; Uibezierpath *eraser_path; Nstimer *mytime; UIImage *tempdraw; Cgcontextref context;-(void) SetTime: (Nstimer *) test_time{if (choose==1) choose=0; else if (choose==0) choose=1; [Self setneedsdisplay];} -(Ibaction) Test_draw: (ID) Sender {mytime=[nstimer scheduledtimerwithtimeinterval:0.2 target:self selector: @selector (settime:) Userinfo:nil repeats:yes];} -(Ibaction) Eraser: (ID) Sender {//pick_color=[uicolor alloc]; [MPath Closepath]; Pick_color=[uicolor Blackcolor]; Choose=-1;} -(ID) initWithFrame: (cgrect) frame{Mpath=[[uibezierpath Bezierpath] init]; Eraser_path=[[uibezierpath Bezierpath]init]; Pick_color=[[uicolor Alloc]init]; Pick_color=[uicolor new]; Pick_color=[uicolor Greencolor]; choose=1; [Self Setbackgroundcolor:[uicolor blackcolor]]; Self.backgroundcolor=[uicolor Blackcolor]; self = [super Initwithframe:frame]; if (self) { Initialization code//SELF init} return self; }/*//only override drawrect:if your perform custom drawing.//an empty implementation adversely affects performance Durin G animation.-(void) DrawRect: (cgrect) rect{//Drawing code}*///int x,y;//int firstx,firsty;/*-(void) DrawRect: (CGRect) rect{cgcontextref Context=uigraphicsgetcurrentcontext (); CGFloat temp=25.4; Cgcontextsetstrokecolorwithcolor (Context,[uicolor Redcolor]. Cgcolor); Cgcontextsetlinewidth (context,5.8); Cgcontextmovetopoint (Context,firstx,firsty); Cgcontextaddlinetopoint (CONTEXT, x, y); Cgcontextaddlines (context,//, < #size_t count#>) Cgcontextstrokepath (context); printf ("drawrect\n");} */-(void) DrawRect1: (cgrect) rect{switch (choose) {case 1:case-1: [Tempdraw drawinrect:rect]; Break Case 0://context=uigraphicsgetcurrentcontext (); [Self drawrect2:rect]; Break }}-(void) DrawRect: (cgrect) rect{//Pick_color=[uicolor alloc]; Uigraphicsbeginimagecontext (self.frame.size); Mpath.linewidth = 15.0; Mpath.linecapstyle = Kcglinejoinround; Line corner mpath.linejoinstyle = kcglinecapround; Cgcontextref Context=uigraphicsgetcurrentcontext (); Uigraphicsbeginimagecontext (self. Cgsizemake); Cgcontextsetblendmode (CONTEXT, kcgblendmodenormal); Cgcontextsetshadowwithcolor (Context,cgsizemake (1, 1), 18.F, [Pick_color Cgcolor]); Uicolor *color =pick_color; [Uicolor Greencolor]; [Color set]; Set Line Color Uicolor *back_color=[uicolor Blackcolor]; uibezierpath* Apath =mpath; [Uibezierpath Bezierpath]; End processing switch (choose) {case 1:[mpath stroke]; Break Case-1: [Eraser_path stroke]; Break Case 0: [Back_color set]; Break }//draws line according to the coordinate point of the connection} static Cgpoint midpoint (Cgpoint p0, CGpoint p1) {return (Cgpoint) {(p0.x + p1.x)/2.0, (P0.Y + p1.y)/2.0};}-(UIImage *) getimage{ Uigraphicsbeginimagecontext (self.bounds.size); [Self.layer Renderincontext:uigraphicsgetcurrentcontext ()]; UIImage *result=uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); return result; }-(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{Uitouch *touch=[touches Anyobject]; Cgpoint Point=[touch locationinview:self]; Cgpoint Mpoint=midpoint (Lastpoint, point); Switch (choose) {case 1:[mpath addquadcurvetopoint:mpoint controlpoint:lastpoint]; Break Case-1: [Eraser_path addquadcurvetopoint:mpoint controlpoint:lastpoint]; Break } Lastpoint=point; int tempx=point.x; int tempy=point.y; NSLog (@ "touches move%d%d \ n", tempx,tempy); Uigraphicsbeginimagecontext (self.frame.size); [Self.layer Renderincontext:uigraphicsgeTcurrentcontext ()]; Context=uigraphicsgetcurrentcontext (); [Tempdraw DrawInRect:self.frame]; [Self drawRect2:self.frame]; UIImage *result=uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); Tempdraw=result; [Self setneedsdisplay]; }-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{//[super touchesbegan:< #touches #> withEvent: < #event #>]; Touches Uitouch//Mpath=[[uibezierpath Bezierpath] init]; if (choose==0) {[MyTime invalidate]; choose=1; } cgpoint point=[[touches Anyobject] locationinview:self]; Firstx=point.x; Firsty=point.y; Switch (choose) {Case 1: [MPath movetopoint:point]; Break Case-1: [Eraser_path movetopoint:point]; } Lastpoint=point; NSLog (@ "%d%d \ n", x, y); [Self setneedsdisplay]; }-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{//[MPath Closepath];} @end