重置撤消/重做阵列iOS

时间:2012-04-23 04:46:52

标签: iphone core-graphics nsundomanager

我想使用NSUndoManager撤消和重做绘图应用中的笔触和更改。 我使用UIGraphicsGetImagefromCurrentImageContext()将图像存储到一个可变数组中。

下面的“我的代码”提供了一个想法:

- (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);
}

我可以如何以及在什么时候清除layerArray,并重置撤消堆栈?提前致谢

2 个答案:

答案 0 :(得分:2)

似乎你可以使用每个类中的方法来清除:

答案 1 :(得分:0)

使用以下参考..

Undo redo in paint app

希望,这会对你有所帮助