【问题标题】:Reset Undo/Redo Array iOS重置撤消/重做数组 iOS
【发布时间】:2023-03-02 23:30:01
【问题描述】:

我想使用 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


【解决方案1】:

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

【讨论】:

  • 我的问题是什么时候应该重置 NSUndoManager 和 NSMutableArray?
  • 嗯,你上面的问题不是这样写的:第一行是“我想重置”,而不是“我什么时候应该重置”。我不清楚实际问题是什么:您的撤消堆栈中是否有不需要的操作?换句话说:为什么要重置它?
  • 我登录时 layerArray 继续增长。当另一组操作替换撤消堆栈中的前一个操作时,我需要一种清除该数组的方法。
猜你喜欢
  • 2016-10-28
  • 2020-01-08
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
相关资源
最近更新 更多