我想使用 NSUndoManager 撤消和重做绘图应用程序中的笔触和更改。我使用 UIGraphicsGetImagefromCurrentImageContext() 将图像存储到可变数组中。
我下面的代码给出了一个想法:
- (void) performUndoRedo:(BOOL) undo
{
if (undo)
{
[undoManager undo];
}
else
{
[undoManager redo];
}
NSLog(@"layerArray:%@", layerArray);
[self drawImage:[layerArray lastObject]];
}
- (void) redo:(id) object
{
[layerArray addObject:object];
[[undoManager prepareWithInvocationTarget:self] undo:object];
[undoManager setActionName:@"drawredo"];
// NSLog(@"layerArray IN REDO:%@", layerArray);
}
- (void) undo:(id) object
{
[[undoManager prepareWithInvocationTarget:self] redo:object];
[undoManager setActionName:@"drawundo"];
[layerArray removeObject:object];
// NSLog(@"layerArray IN UNDO:%@", layerArray);
}
- (void) touchesEnded:(NSSet *) touches withEvent:(UIEvent *) event
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
[layerArray addObject:layerImage];
[self undo:layerImage];
UIGraphicsEndImageContext();
NSLog(@"%@", layerArray);
}
我如何以及在什么时候清除 layerArray 并重置撤消堆栈?提前致谢