1

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

我有 touchesBegan 和移动的这段代码:

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


}

我认为,如果我能弄清楚如何填充撤消堆栈,我就可以遍历堆栈以撤消重做触摸。也许我都湿透了。我当然会感谢一些帮助...

谢谢

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

4

1 回答 1

0

我终于通过管理数组解决了这个问题。

对于每个笔划,都有一个缓冲区数组:

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

然后 Undo 和 Redo 方法如下所示:

-(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?”

  1. @property (strong,nonatomic) DrawingPath *currentColoredPath;

  2. 这里指的是一个DrawingPath类,我写的如下:

。H

#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;

.m

#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 );

}
于 2012-05-17T05:42:07.743 回答