我想使用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并重置撤消堆栈?在此先感谢
似乎您可以使用每个类中的方法进行清除:
layerArray
是NSMutableArray
NSMutableArray
的文档清空所有元素的数组。
[removeAllObjects
是removeAllObjects
:
undoManager
的文档:清除撤消和重做堆栈并重新启用接收器。
有人在绘图应用程序上有撤消/重做选项的完整示例吗?请与我分享。