1

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 をクリアし、元に戻すスタックをリセットできますか? 前もって感謝します

4

1 に答える 1

2

これらの各クラスのメソッドを使用してクリアできるようです。

  • layerArrayですNSMutableArray

    • のドキュメントからremoveAllObjects

      配列のすべての要素を空にします。

  • undoManagerNSUndoManagaer:

    • のドキュメントからremoveAllActions

      元に戻すスタックとやり直しスタックをクリアし、レシーバーを再度有効にします。

于 2012-04-23T05:01:19.917 に答える