Ganesh Nayak
Ganesh Nayak

Reputation: 798

Reset Undo/Redo Array iOS

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

Answers (1)

ckhan
ckhan

Reputation: 4791

Seems like you can use the methods in each of those classes for clearing:

Upvotes: 2

Related Questions