Reputation: 798
I want to undo and redo the strokes and changes in a drawing app, using NSUndoManager. I am storing the image using UIGraphicsGetImagefromCurrentImageContext() onto a mutable array.
My Code below gives an idea:
- (void) performUndoRedo:(BOOL) undo
{
if (undo)
{
[undoManager undo];
}
else
{
[undoManager redo];
}
NSLog(@"layerArray:%@", layerArray);
[self drawImage:[layerArray lastObject]];
}
- (void) redo:(id) object
{
[layerArray addObject:object];
[[undoManager prepareWithInvocationTarget:self] undo:object];
[undoManager setActionName:@"drawredo"];
// NSLog(@"layerArray IN REDO:%@", layerArray);
}
- (void) undo:(id) object
{
[[undoManager prepareWithInvocationTarget:self] redo:object];
[undoManager setActionName:@"drawundo"];
[layerArray removeObject:object];
// NSLog(@"layerArray IN UNDO:%@", layerArray);
}
- (void) touchesEnded:(NSSet *) touches withEvent:(UIEvent *) event
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
[layerArray addObject:layerImage];
[self undo:layerImage];
UIGraphicsEndImageContext();
NSLog(@"%@", layerArray);
}
How and at what point of action can I clear the layerArray, and reset the undo stack? Thanks in advance
Upvotes: 1
Views: 1886
Reputation: 4791
Seems like you can use the methods in each of those classes for clearing:
layerArray
is an NSMutableArray
removeAllObjects
Empties the array of all its elements.
undoManager
is an NSUndoManagaer
:
removeAllActions
:
Clears the undo and redo stacks and re-enables the receiver.
Upvotes: 2