swift - Adding undo function to drawing app -


i followed raywenderlich tutorial on using uikit make drawing app. i'm trying add in functionality undo last stroke. ideally undo 10ish strokes. i'm trying figure out best way go doing this. thinking of creating imageview has last stroke , making imageview.image = nil when user presses back. in code tutorial there's similar this. when touches end, newest stroke merged onto imageview of old ones @ right opacity. i'm not sure how add third (and potentially more) imageivews code make work. ideas / better way go this? code touchesended below.

code

override func touchesended(touches: set<nsobject>, withevent event: uievent) {          if !swiped {             // draw single point             drawlinefrom(lastpoint, topoint: lastpoint)         }          // merge tempimageview mainimageview         uigraphicsbeginimagecontext(mainimageview.frame.size)         mainimageview.image?.drawinrect(cgrect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendmode: kcgblendmodenormal, alpha: 1.0)         tempimageview.image?.drawinrect(cgrect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendmode: kcgblendmodenormal, alpha: opacity)         mainimageview.image = uigraphicsgetimagefromcurrentimagecontext()         uigraphicsendimagecontext()          tempimageview.image = nil     } 

@spaceshroomies: found/came great solution..

i used parts nshiptser, , raywenderlich tutorial.

and here solution:

override func touchesended(touches: set<nsobject>, withevent event: uievent) {     //undomanager     let undo: uiimageview = undomanager?.preparewithinvocationtarget(temp2image) as! uiimageview      uigraphicsbeginimagecontext(temp2image.frame.size)     temp2image.image?.drawinrect(cgrect(x: 0, y: 0, width: temp2image.frame.size.width, height: temp2image.frame.size.height), blendmode: kcgblendmodenormal, alpha: 1.0)     tempimage.image?.drawinrect(cgrect(x: 0, y: 0, width: tempimage.frame.size.width, height: tempimage.frame.size.height), blendmode: kcgblendmodenormal, alpha: opacity)     undo.image = temp2image.image     temp2image.image = uigraphicsgetimagefromcurrentimagecontext()     uigraphicsendimagecontext()      tempimage.image = nil } 

the important thing here set, undo should go i.e. undo.image = temp2image.image otherwise not change "it"


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -