将撤消/重做方法添加到核心图形绘制应用



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

我目前所处的位置如下:

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

[undoManager setLevelsOfUndo:99];视图加载:

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];
}

我得到"那没用"...

我将不胜感激。当我弄清楚我做错了什么时,我会发布我原始问题的答案。

---编辑---

我已经更改了重置图像,如下所示:

- (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"键变得不稳定也无济于事)我昨天有一个小孙女确实有帮助:))))

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

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

View 方法

调用绘图方法来实际执行绘制,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;
}

相关内容

  • 没有找到相关文章

最新更新