ios - how to undo the line draw using CGContextAddLineToPoint? -
i'm developing 1 brush application freehand drwing , i'm new in core graphics framework.
right application ready drawing using below code
override func touchesbegan(touches: nsset!, withevent event: uievent!) { println("enter in touchesbegan method"); mouseswiped = false; var touch : uitouch = touches.anyobject() uitouch; lastpoint = touch.locationinview(self.view); } override func touchesmoved(touches: nsset!, withevent event: uievent!) { println("enter in touchesmoved method"); mouseswiped = true; var touch : uitouch = touches.anyobject() uitouch; var currentpoint : cgpoint = touch.locationinview(self.view); uigraphicsbeginimagecontext(self.view.frame.size); self.tempdrawimage!.image?.drawinrect(cgrect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)); println("lastpoint.x :\(lastpoint!.x) , lastpoint.y :\(lastpoint!.y)") cgcontextmovetopoint(uigraphicsgetcurrentcontext(), lastpoint!.x, lastpoint!.y); println("currentpoint.x :\(currentpoint.x) , currentpoint.y :\(currentpoint.y)") cgcontextaddlinetopoint(uigraphicsgetcurrentcontext(), currentpoint.x, currentpoint.y); cgcontextsetlinecap(uigraphicsgetcurrentcontext(), kcglinecapround); cgcontextsetrgbstrokecolor(uigraphicsgetcurrentcontext(), red!, green!, blue!, 1.0) cgcontextsetlinewidth(uigraphicsgetcurrentcontext(), brush!); cgcontextsetblendmode(uigraphicsgetcurrentcontext(), kcgblendmodenormal); cgcontextstrokepath(uigraphicsgetcurrentcontext()); //contexts!.addobject(uigraphicsgetcurrentcontext()); self.tempdrawimage!.image = uigraphicsgetimagefromcurrentimagecontext(); tempdrawimage!.alpha = opacity!; uigraphicsendimagecontext(); println("dot"); lastpoint = currentpoint; } override func touchesended(touches: nsset!, withevent event: uievent!) { println("enter in touchesended method"); if mouseswiped == false { println("false"); uigraphicsbeginimagecontext(self.view.frame.size); self.tempdrawimage!.image?.drawinrect(cgrect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)); cgcontextsetlinecap(uigraphicsgetcurrentcontext(), kcglinecapround); cgcontextsetlinewidth(uigraphicsgetcurrentcontext(), brush!); cgcontextsetrgbstrokecolor(uigraphicsgetcurrentcontext(), red!, green!, blue!, opacity!); cgcontextmovetopoint(uigraphicsgetcurrentcontext(), lastpoint!.x, lastpoint!.y); cgcontextaddlinetopoint(uigraphicsgetcurrentcontext(), lastpoint!.x, lastpoint!.y); cgcontextstrokepath(uigraphicsgetcurrentcontext()) cgcontextflush(uigraphicsgetcurrentcontext()); self.tempdrawimage!.image = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); } uigraphicsbeginimagecontext(self.mainimage!.frame.size); self.mainimage!.image?.drawinrect(cgrect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height ), blendmode: kcgblendmodenormal, alpha: 1.0); self.tempdrawimage!.image.drawinrect(cgrect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height), blendmode: kcgblendmodenormal, alpha: opacity!); self.mainimage!.image = uigraphicsgetimagefromcurrentimagecontext(); self.tempdrawimage!.image = nil uigraphicsendimagecontext(); }
but in application see give undo functionality in application want give facility if utilize draw 2 line , press undo button lastly added line remove 1 1 image view
i searched can't understand , none of reply accepted i'm confused please help me.
thank you!
in situation since adding lines becomes simple. need create mutable array contain points user input.
now on every touches begin or whatever utilize in end create nsvalue
point got in code , add together value array mentioned previously. after added line need phone call "refresh" method or rather yet tell view redisplay using setneedsdisplay
trigger draw rect method called. rest of code moved method...
now in target undo button remove lastly 2 points array (i hope no problem here) , phone call refresh or redisplay (same on touch event).
so either move drawing code "refresh" method or "draw rect" need first clear context background, iterate through array of points , draw each of line.
simple enough?
ios core-graphics cgcontext
No comments:
Post a Comment