6

I am working on an app and i have to draw a smooth line so I followed this link to smooth the line iPhone smooth sketch drawing algorithm and I followed the answer of kyoji. But now I don't know how to implement Undo Redo functionality.

Please help

Community
  • 1
  • 1
Mashhadi
  • 3,004
  • 3
  • 46
  • 80
  • have you tried using the built in class NSUndoManager – MCKapur Mar 29 '12 at 13:24
  • this is Available in Mac OS X v10.0 and later not in iOS. and my app is in iOS – Mashhadi Apr 02 '12 at 16:36
  • 2
    and iOS 3.0 and later: https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSUndoManager_Class/Reference/Reference.html –  Apr 03 '12 at 20:57
  • In My app I am drawing lines. this is not the problem that how to revert, problem is that which step to be revert? – Mashhadi Apr 04 '12 at 06:55
  • I am implemented in my project as signature functionality. Is it work for you, there is also have functionality to remove that on double click. – sandy Apr 05 '12 at 06:43
  • @sandy it will remove completely or the last stroke? kindly show me whatever it is. – Mashhadi Apr 05 '12 at 09:51
  • maybe the can help you : http://stackoverflow.com/questions/6281789/opengl-es-undo-in-a-pixel-painting-app/9764796#9764796 – charse Apr 07 '12 at 03:13
  • @charse thanx a lot but this is for open GL. well I'll try this logic in my scenario. may this thing help me – Mashhadi Apr 07 '12 at 18:43
  • @Mashhadi it will remove completely on double click. – sandy Apr 09 '12 at 06:49
  • Ok but tell me the method how it will work. it may help me – Mashhadi Apr 09 '12 at 07:03
  • @Mashhadi plz give me your emailId on my GmailId. I will give you a sample project. – sandy Apr 10 '12 at 05:18
  • I cannot able to add details about the code here because it will take a lot of lines of code, – sandy Apr 10 '12 at 05:20
  • okay then kindly go and answer this Question and write all the code No problem at all. and you will get 50 Reputation points. – Mashhadi Apr 10 '12 at 08:05

1 Answers1

0

Use the NSUndoManager. However, if you are painting lines on the canvas, you will also need to keep their representation around as well (so you can pop them off).

So, whether you collect them as a UIBezierPath, or use shape layers, or your own "array of points" you undo in the same manner.

So, while drawing the line, keep a record of the points you used in your drawing. When the drawing is done (e.g., touchesEnded), you want to "push" your drawing, and tell the undo manager how to undo it. Simply, it would be something like this almost-code...

- (void)pushDrawing:(Drawing*)drawing
{
    [self.stack push:drawing];
    [self.undoManager registerUndoWithTarget: self
                                    selector: @selector(popDrawing)
                                      object: nil];
}

- (void)popDrawing:(Drawing*)drawing
{
    Drawing *drawing = [self.stack pop];
    [self.undoManager registerUndoWithTarget: self
                                    selector: @selector(pushDrawing:)
                                      object: drawing];
}

If you are using one canvas, you may have to redraw the entire thing, especially when popping a drawing off. If you are using views or layers, you may not...

Look at the docs for NSUndoManager... it's available on iOS, and has good examples. It "remembers" if you you undo-ing or redo-ing, and will do the right thing, so the above could be implemented as one function (but it's easier to understand at first with one function going each direction).

Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
  • Hello @JodyHagins, I am also facing the problem of undo and redo, I am drawing to CGLayers, for optimization. I have tried some code, but dont know, where I am goin wrong, can you please look at the code, below is the link http://stackoverflow.com/questions/11394839/undo-redo-issues-with-cglayer . Waiting for your reply, thanks – Ranjit Jul 12 '12 at 09:39