Define a UIView: The main is to add a uiimageview in this view, drawing all in this uiimageview inside
@property (nonatomic) cgpoint prepoint; The point at which the finger precedes the move event @property (nonatomic) cgpoint oppsitepoint; The point of the finger before entering the move event @property (nonatomic, retain) uiimageview* drawimage;-(ID) initWithFrame: (cgrect) frame{self = [Super Initwithframe:frame]; if (self) { ///initialization code self.drawimage = [[Uiimageview alloc] initwithimage:nil]; Self.drawImage.frame = Self.frame; [Self addsubview:_drawimage]; } return self;}
Then handle the finger events
#pragma mark-deal touch-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{NSLog (@ "Touchesbegan"); The following two sentences know the point of the finger on the screen of the information uitouch* touch = [touches anyobject]; Cgpoint point = [Touch locationinview:self]; if (touch) {self.prepoint = point; _oppsitepoint = point; _oppsitepoint.x = 320.0f-point.x; }}-(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{NSLog (@ "touchesmoved"); uitouch* touch = [touches anyobject]; if (touch) {Cgpoint point = [Touch locationinview:self]; Uigraphicsbeginimagecontext (self.frame.size); [_drawimage.image drawinrect:cgrectmake (0, 0, _drawimage.frame.size.width, _drawimage.frame . Size.Height)]; Cgcontextsetlinecap (Uigraphicsgetcurrentcontext (), kcglinecapround); Cgcontextsetlinewidth (Uigraphicsgetcurrentcontext (), 2.0f); Cgcontextsetrgbstrokecolor (Uigraphicsgetcurrentcontext (), 0.314, 0.486, 0.859, 1.0); Cgcontextbeginpath (Uigraphicsgetcurrentcontext ()); Cgcontextmovetopoint (Uigraphicsgetcurrentcontext (), _prepoint.x, _PREPOINT.Y); Cgcontextaddquadcurvetopoint (Uigraphicsgetcurrentcontext (), _prepoint.x, _prepoint.y, Point.x, POINT.Y); Cgcontextmovetopoint (Uigraphicsgetcurrentcontext (), _oppsitepoint.x, _OPPSITEPOINT.Y); Cgcontextaddquadcurvetopoint (Uigraphicsgetcurrentcontext (), _oppsitepoint.x, _oppsitepoint.y, 320.0f-point.x, POINT.Y); Cgcontextaddlinetopoint (Uigraphicsgetcurrentcontext (), Point.x, POINT.Y); Cgcontextstrokepath (Uigraphicsgetcurrentcontext ()); _drawimage.image = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); _prepoint = point; _oppsitepoint = point; _oppsitepoint.x = 320.0f-point.x; }}-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{NSLog (@ "touchesended"); The effect of this paragraph is if the screenClick to draw a point uigraphicsbeginimagecontext (self.frame.size); [_drawimage.image drawinrect:cgrectmake (0, 0, _drawimage.frame.size.width, _drawimage.frame . Size.Height)]; Cgcontextsetlinecap (Uigraphicsgetcurrentcontext (), kcglinecapround); Cgcontextsetlinewidth (Uigraphicsgetcurrentcontext (), 3.0f); Cgcontextsetrgbstrokecolor (Uigraphicsgetcurrentcontext (), 0.314, 0.486, 0.859, 1.0); Cgcontextbeginpath (Uigraphicsgetcurrentcontext ()); Cgcontextmovetopoint (Uigraphicsgetcurrentcontext (), _prepoint.x, _PREPOINT.Y); Cgcontextaddlinetopoint (Uigraphicsgetcurrentcontext (), _prepoint.x, _PREPOINT.Y); Cgcontextstrokepath (Uigraphicsgetcurrentcontext ()); _drawimage.image = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); Self.istouch = NO;}
Below is