virantporwal
virantporwal

Reputation: 987

how to implement Undo feature in drawing so that user can clear his last line

I am using CoreGraphics to implement free hand drawing which is working fine for me and now I want to implement Undo feature for this drawing so that user can clear his last strok

Upvotes: 3

Views: 191

Answers (3)

Hemang
Hemang

Reputation: 1234

In your method:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    if(imageView2.image != nil){
        [pathArray addObject:imageView2.image];
    }

}

And on undo button:

if([pathArray count]>0)
{
    [pathArray removeLastObject];
    if([pathArray count]==0)
    {
        imageView2.image = GlobalImage2;
    }
    else
    {
        imageView2.image=[pathArray objectAtIndex:[pathArray count]-1];
    }

}

Retrive this image from array. I have used same code in my few apps. I hope it'll be useful for you.

Thanks,

Hemang.

Upvotes: 5

Ramon Chiara
Ramon Chiara

Reputation: 656

You should read about the Memento Pattern. Some links:

In time, sorry for not explaining it here. But there are tons of books and articles about it.

Upvotes: 1

Related Questions