我正在尝试使用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];
}
我得到“那不起作用”......
我很感激帮助。当我弄清楚我做错了什么时,我会将答案发给我原来的问题。
--- --- EDIT
我更改了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;
}
}
然而,混乱下雨 - 如果有人(布拉德?,贾斯汀?)可以提供我需要采取的步骤的子弹点列表,这可能会有所帮助。例如:
。在...中添加通知 。触发通知.... 。撤消/重做按钮指向... 。我真正需要构建哪些方法/功能.. ....
我希望这会让我给你更多的积分而不是1 ..
(我的“o”键变硬了没有帮助) 我确实帮助我昨天生了一个小孙女:))))
答案 0 :(得分:1)
首先,我要感谢大家的任何/所有帮助。我终于解决了这个问题,虽然我不确定它是否是最好的解决方案。
我从UIViewController调用了一个UIView。控件(颜色和画笔)保留在VC中。绘图方法移至View方法。
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;
}