3

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
Nari
  • 213
  • 5
  • 16
  • 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 Answers4

3

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..

rohan-patel
  • 5,772
  • 5
  • 45
  • 68
0

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.

JL.LAO
  • 11
  • 1
  • 4
0

Here's the Memento pattern.

Jeffrey Zhao
  • 4,923
  • 4
  • 30
  • 52
0

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.

Community
  • 1
  • 1
Wint
  • 2,268
  • 2
  • 22
  • 32