重置撤消/重做阵列iOS

问题描述 投票:1回答:3

我想使用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并重置撤消堆栈?在此先感谢

iphone core-graphics nsundomanager
3个回答
2
投票

似乎您可以使用每个类中的方法进行清除:

  • layerArrayNSMutableArray

    • 来自NSMutableArray的文档

      清空所有元素的数组。

  • [removeAllObjectsremoveAllObjects

    • [来自undoManager的文档:

      清除撤消和重做堆栈并重新启用接收器。


0
投票

使用以下参考。

NSUndoManagaer

希望,对您有帮助


0
投票

有人在绘图应用程序上有撤消/重做选项的完整示例吗?请与我分享。

© www.soinside.com 2019 - 2024. All rights reserved.