I have a drawing app in which I want to implement UNDO feature so that user can undo the last drawing stroke that has been done.
Can you please let me know how can this be done?
Thanks
I have a drawing app in which I want to implement UNDO feature so that user can undo the last drawing stroke that has been done.
Can you please let me know how can this be done?
Thanks
First of all please specify what do you use to draw. Do you use any API or Core-Graphics Framework for drawing.
Here are some of the approaches from the link I found.
Approach:
on touchesEnded, save the image to app sandbox with a name like.. 1.png, 2.png etc. then put a count on "undo" variable. when user press undo, just loads the old image base on the count. (this is what I can think of roughly, but haven't gone through the coding of it yet, but sounds pretty simple to code).
NSUndoManager.
Hope this helps for anyone else looking to do this.
Reference Link:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/46996-drawing-app-how-do-undo.html
Cheers
My simple solution is , 1. Store the CGPath . 2. You can use clear blend mode to draw the path to clear it.
NON iPhone specific
Pixel canvas:
Your option would be to keep a snapshot of either the whole canvas or the region that got covered by the stroke in a stack. the undo action would pop your undo stack and redraw the buffer you stacked in.
The inconvenient would be to properly size your undo limit to avoid hogging the memory.
Vector drawing:
It is pretty much trivial ....
It depends on alot of things, you have to be more specific about what drawing method you are using, for example if you are talking about "pencil" strokes and your drawing method uses quartz to just draw something, and you are only interested in the last stroke or change, you could always save the current graphics context (image context or something) and present that in case of an undo. If you want to allow several strokes to be undone you would have to add them to an array so you can redraw up to the undo. If you are using open gl then its also different.