【发布时间】:2023-03-02 23:30:01
【问题描述】:
我想使用 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 并重置撤消堆栈?提前致谢
【问题讨论】:
-
撤消重做操作是否成功
-
你有没有找到任何解决方案。我也面临同样的问题。如果有人对此有任何想法,那将对我有所帮助。
标签: iphone core-graphics nsundomanager