2

我正在尝试使用 NSUndoManager 实现撤消/重做方法。我已经对此提出了其他问题,但仍然卡住了。

我现在的位置如下:

.h
NSUndoManager *undoManager;
@property(nonatomic,retain) NSUndoManager *undoManager;

.m
@synthesize undoManager;

[undoManager setLevelsOfUndo:99]; viewdidload:

NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];      
[dnc addObserver:self selector:@selector(undoButtonTapped) name:@"undo" object:nil];   
[dnc addObserver:self selector:@selector(redoButtonTapped) name:@"redo" object:nil];

- (void)resetTheImage:(UIImage*)image
{
    NSLog(@"%s", __FUNCTION__);

   // image = savedImage.image;
    if (image != drawImage.image)
    {
        [[undoManager prepareWithInvocationTarget:self] resetTheImage];
        image = drawImage.image ;        
    } else {
        NSLog(@"That didn't work");
    }
}

- (void)undoButtonTapped {
    NSLog(@"%s", __FUNCTION__);
    [undoManager undo];
}

我得到“那没有用”......

我会很感激帮助。当我弄清楚我做错了什么时,我会发布我原来问题的答案。

- -编辑 - -

我已将 resetTheImage 更改如下:

- (void)resetTheImage:(UIImage*)image
{
    NSLog(@"%s", __FUNCTION__);

    image = savedImage.image;
    if (image != drawImage.image)
    {
        drawImage.image = image;
        savedImage.image = image;
        NSLog(@"undo image");
        [[self.undoManager prepareWithInvocationTarget:drawImage.image] image];        

    } else {
        NSLog(@"same image");
        savedImage.image = image;
    }
}

但是,混乱下雨了——如果有人(布拉德?贾斯汀?)可以提供一个项目符号列表,列出我需要采取哪些步骤来完成这项工作,这可能会有所帮助。例如:

. 在...中添加通知。触发通知...... 让撤消/重做按钮指向...。我真正需要构建哪些方法/功能......

我希望 SO 能让我给你的分数超过 1..

(我的“o”键变得不稳定并没有帮助)昨天我有一个小孙女确实有帮助:))))

4

1 回答 1

1

首先,我要感谢大家的任何/所有帮助。我终于解决了这个问题,虽然我不确定这是否是最好的解决方案。

我制作了一个从 UIViewController 调用的 UIView。控件(颜色和画笔)保留在 VC 中。绘图方法移至视图方法。

View 方法调用 Drawing 方法来实际执行绘制,而 View 方法控制撤消/重做。

以下是一些代码片段:

-(void)undoButtonClicked
{
    //NSLog(@"%s", __FUNCTION__);
    if ([self.currentArray count] == 0) {
        //nothing to undo
        return;
    }

    DrawingPath *undonePath = [self.currentArray lastObject];
    [self.currentArray removeLastObject];
    [self.redoStack addObject:undonePath];
    [self setNeedsDisplay];

}

-(void)redoButtonClicked
{
    //NSLog(@"%s", __FUNCTION__);

    if ([self.redoStack count] == 0) {
        // nothing to redo
        return;
    }

    DrawingPath *redonePath = [self.redoStack lastObject];
    [self.redoStack removeLastObject];
    [self.currentArray addObject:redonePath];
    [self setNeedsDisplay];

}

让我知道是否有人想要澄清。再次感谢大家。。

按要求更新:

这些是一些标题:

    DrawingViewController  *mvc;
    NSMutableArray *pathArray;
    NSMutableArray *colorArray;
    NSMutableArray *bufferArray;
    NSMutableArray *currentArray;
    UIBezierPath *myPath;
    NSString *brushSize;
    CGPoint lastPoint;
    int colorIndex;
    NSString *colorKey;

    SoundEffect         *erasingSound;
    SoundEffect         *selectSound;

    BOOL swiped;    
    int moved;
    UIColor *currentColor;
    NSString *result;
}
@property(nonatomic,assign) NSInteger undoSteps;
@property (strong, nonatomic) NSString *result;

@property (strong,nonatomic) UIColor *currentColor;
@property (strong,nonatomic) NSMutableArray *currentArray;
@property (strong,nonatomic) NSMutableArray *bufferArray;
@property (strong,nonatomic) DrawingPath *currentColoredPath;
@property (strong,nonatomic) NSMutableArray *redoStack;
@property (strong, nonatomic) NSString *colorKey;

这里还有一些方法.. currentArray 然后在一种堆栈中跟踪点、画笔和颜色。撤消从堆栈中删除,并添加到可用于重做的临时堆栈中。

-(void)undoButtonClicked
{
    //NSLog(@"%s", __FUNCTION__);
    if ([self.currentArray count] == 0) {
        //nothing to undo
        return;
    }

    DrawingPath *undonePath = [self.currentArray lastObject];
    [self.currentArray removeLastObject];
    [self.redoStack addObject:undonePath];
    [self setNeedsDisplay];

}

-(void)redoButtonClicked
{
    //NSLog(@"%s", __FUNCTION__);

    if ([self.redoStack count] == 0) {
        // nothing to redo
        return;
    }

    DrawingPath *redonePath = [self.redoStack lastObject];
    [self.redoStack removeLastObject];
    [self.currentArray addObject:redonePath];
    [self setNeedsDisplay];

}


#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   
    //NSLog(@"%s", __FUNCTION__);
    self.currentColoredPath = [[DrawingPath alloc] init];
    [self.currentColoredPath setColor:self.currentColor];
    UITouch *touch= [touches anyObject];


    [self.currentColoredPath.path moveToPoint:[touch locationInView:self]];
    [self.currentArray addObject:self.currentColoredPath];
    // Remove all paths from redo stack
    [self.redoStack removeAllObjects];

    lastPoint = [touch locationInView:self];
    lastPoint.y -= 20;

    if ([touch tapCount] == 2) {
        [self alertOKCancelAction];

        return;
    }  


}



-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //NSLog(@"%s", __FUNCTION__);
    UITouch *touch = [touches anyObject]; 
    [self.currentColoredPath.path addLineToPoint:[touch locationInView:self]];

    [self setNeedsDisplay];


    CGPoint currentPoint = [touch locationInView:self];
    currentPoint.y -= 20;


    lastPoint = currentPoint;

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //NSLog(@"%s", __FUNCTION__);
    self.currentColoredPath = nil;
}
于 2012-04-21T17:08:20.757 回答