Cocoa touch 如何在iOS/core图形中构建撤消堆栈

Cocoa touch 如何在iOS/core图形中构建撤消堆栈,cocoa-touch,ios5,core-graphics,Cocoa Touch,Ios5,Core Graphics,我正在尝试向一组触摸添加撤消/重做功能 我有触摸开始和移动的代码: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"%s", __FUNCTION__); mouseSwiped = NO; UITouch *touch = [touches anyObject]; if ([touch tapCount] == 2) { [self erase

我正在尝试向一组触摸添加撤消/重做功能

我有触摸开始和移动的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s", __FUNCTION__);
    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        [self eraseButtonTapped:self];
        return;
    }

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

    [self.undoPath addObject:WHATGOESHERE];
    // Remove all paths from redo stack
    [self.redoStack removeAllObjects];

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    //NSLog(@"%s", __FUNCTION__);
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y -= 20;



    UIGraphicsBeginImageContext(self.view.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, brush);
    CGContextSetRGBStrokeColor(context, red, green, blue, 1.0);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
    CGContextStrokePath(context);
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.undoPath addObject:WHATGOESHERE];
    UIGraphicsEndImageContext();


    NSLog(@"Touches Moved undoPath contains %i objects", [self.undoPath count]);
    // Remove all paths from redo stack
    [self.redoPath removeAllObjects];
    lastPoint = currentPoint;


}
我认为,如果我能想出如何填充撤销堆栈,我就可以在堆栈中迭代撤销重做。。也许我全身湿透了。我当然会感谢你的帮助

谢谢


…我以前也问过类似的问题,但我以另一种形式重新启动了项目,因为最后一种方式并不令人满意。

我最终通过管理阵列解决了这个问题

对于每个笔划,在缓冲区数组中添加一个:

[self.currentColoredPath.path moveToPoint:[touch locationInView:self]];
[self.currentArray addObject:self.currentColoredPath];
// Remove all paths from redo stack
[self.redoStack removeAllObjects];
然后,撤消和重做方法如下所示:

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

}
我希望这能帮助别人

更新-这是对以下问题的回答:什么是currentColoredPath

@属性强,非原子DrawingPath*currentColoredPath

这是一个类DrawingPath,我编写如下:

h


有一个内置的iOS。看看那个?谢谢。我刚读到这个。它可能非常有用,但是一个代码片段会非常有用。我个人觉得从代码中学习要比从文档中学习容易得多。我花了数小时/数天的时间来探索这一点。我还没有找到一个全面的答案。如果我收集了一个,我将发布我的解决方案。我能报答别人对我的帮助的唯一方法就是帮助别人。这里什么是currentColoredPath.path你能给我解释一下吗
#import <Foundation/Foundation.h>

@interface DrawingPath : NSObject {
    NSString *brushSize;
}
@property (strong, nonatomic) NSString *brushSize;
@property (strong,nonatomic) UIColor *color;
@property (strong,nonatomic) UIBezierPath *path;

- (void)draw;
- (void)brushChange;
#import "DrawingPath.h"

@implementation DrawingPath

@synthesize path = _path;
@synthesize color = _color;
@synthesize brushSize = _brushSize;
float brush = 12;

- (id)init {
    //NSLog(@"%s", __FUNCTION__);
    if (!(self = [super init] ))
        return nil;
    brushSize = [[NSUserDefaults standardUserDefaults] objectForKey:@"brushKey"];
    [self brushChange];


    _path = [[UIBezierPath alloc] init];
    _path.lineCapStyle=kCGLineCapRound;
    _path.lineJoinStyle=kCGLineJoinRound;

    [_path setLineWidth:brush];





    return self;
}

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

    [self.color setStroke];
    [self.path stroke];
}

- (void)brushChange { //from notification
    //NSLog(@"%s", __FUNCTION__);

    brushSize = [[NSUserDefaults standardUserDefaults] objectForKey:@"brushKey"];
    //NSLog(@"DrawingPath - brushSize is %@: ", brushSize );


    //NSLog(@"DrawingPath - brush is %f: ", brush );

}