我正在尝试为一组触摸添加撤消/重做功能..
我有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;
}
我认为如果我能想出如何填充撤销堆栈,我可以在堆栈中迭代以撤消重做触摸......也许我已经湿了。我当然会感激一些帮助...
由于
..之前我曾问过类似的问题,但我已经以不同的形式重新启动了项目,因为最后一种方式并不令人满意。
答案 0 :(得分: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?”
@property (strong,nonatomic) DrawingPath *currentColoredPath;
这是指一个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 );
}