I am doing a painting app. In that, I draw some image. But I want to add undo operation to remove the previous drawn item. How can I implement that undo operation?
-
How do you store the objects that have been drawn? The answer highly depends on your code. Core Data has a nice undo/redo mechanism. – dasdom Dec 10 '11 at 09:51
-
i added one button for saving that drawn image. so, when i hit save button, then its stored into core data. – Nari Dec 10 '11 at 10:07
4 Answers
There is an application named TouchPainter and its source code is available. It contains drawing, color blending, Undo/Redo (Amazing..!!), Save/Open drawings..
Note : It may require very deep level knowledge of Objective C. The whole application's source code is explained in this book "Apress.Pro.Objective-C.Design.Patterns.for.iOS.Mar.2010" available and source code is also available here :
http://www.apress.com/apple-mac/objective-c/9781430233305
I hope it will be helpful to you.. :) Good luck..

- 5,772
- 5
- 45
- 68
I would create an NSMutableArray adding each operation on it.
NSMutableArray *ObjectList = [[NSMutableArray alloc] init];
[ObjectList addObject: TheSKSprite1];
[ObjectList addObject: TheSKSprite2];
[ObjectList addObject: TheSKSprite3];
...
then retrieve the last Drawing added, to removed it from the View and from the Array.
SKSpriteNode *OneDrawing = [ObjectList objectAtIndex:(int)[ObjectList count]-1];
[OneDrawing removeFromParent];
[ObjectList removeObjectAtIndex:(int)[ObjectList count]-1];
I hope it could helps you.

- 11
- 1
- 4
refer to this link: Design Pattern for Undo Engine
Generally speaking, you need to implement a Command Pattern(or a similar one). If one operation can be reversed,(for example, in graphics program, you scale up a circle), you can store the modification in the command. else, you might have to save a deep clone of the previous state.