gpt4 book ai didi

ios - 使用 CGLayer 撤消重做问题

转载 作者:行者123 更新时间:2023-11-29 13:32:54 24 4
gpt4 key购买 nike

我在 CgLayer 上使用 unod 重做操作,我已经尝试了一些代码,但无法让它工作,不知道,我哪里错了,下面是我写的代码

这是我的 drawRect 函数

- (void)drawRect:(CGRect)rect
{
m_backgroundImage = [UIImage imageNamed:@"bridge.jpg"];

CGPoint drawingTargetPoint = CGPointMake(0,0);
[m_backgroundImage drawAtPoint:drawingTargetPoint];


switch(drawStep)
{
case DRAW:
{
CGContextRef context = UIGraphicsGetCurrentContext();

if(myLayerRef == nil)
{

myLayerRef = CGLayerCreateWithContext(context, self.bounds.size, NULL);
}

CGContextDrawLayerAtPoint(context, CGPointZero, myLayerRef);
break;
}

case UNDO:
{
[curImage drawInRect:self.bounds];
break;
}

default:
break;
}
}

在触摸结束时,我将层转换为 NSValue 并将作为键值对存储到 NSDictionary 中,然后将字典对象添加到数组中。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSValue *layerCopy = [NSValue valueWithPointer:myLayerRef];


NSDictionary *lineInfo = [NSDictionary dictionaryWithObjectsAndKeys:layerCopy, @"IMAGE",
nil];

[m_pathArray addObject:lineInfo];
NSLog(@"%i",[m_pathArray count]);

}

下面是我的撤消功能

- (void)undoButtonClicked
{
if([m_pathArray count]>0)
{
NSMutableArray *_line=[m_pathArray lastObject];
[m_bufferArray addObject:[_line copy]];
[m_pathArray removeLastObject];
drawStep = UNDO;
[self redrawLine];
}
}

//Redraw functions

- (void)redrawLine
{
NSDictionary *lineInfo = [m_pathArray lastObject];

NSValue *val = [lineInfo valueForKey:@"IMAGE"];

CGLayerRef layerToShow = (CGLayerRef) [val pointerValue];

CGContextRef context1 = CGLayerGetContext(layerToShow);
CGContextDrawLayerAtPoint(context1, CGPointMake(00, 00),layerToShow);
[self setNeedsDisplayInRect:self.bounds];
}

我认为这是我出错的地方。所以 friend 们请帮帮我。

从下面的评论中,我添加了函数,它绘制到 Cglayer 中(这个函数我正在调用 touchesMovedEvent。

- (void) drawingOperations
{
CGContextRef context1 = CGLayerGetContext(myLayerRef);

CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2);
CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);

CGContextMoveToPoint(context1, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context1, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context1, kCGLineCapRound);
CGContextSetLineWidth(context1, self.lineWidth);
CGContextSetStrokeColorWithColor(context1, self.lineColor.CGColor);
CGContextSetAllowsAntialiasing(context1, YES);
CGContextSetInterpolationQuality(context1, kCGInterpolationHigh);
CGContextSetAlpha(context1, self.lineAlpha);
CGContextStrokePath(context1);
}

问候兰 git

最佳答案

实现 Undo 和 Redo 的最好方法是实现 NSUndoManager 作为对它的简要描述你不必保存你想要撤销或重做的对象的每个状态 NSUndoManager 本身使这个给你...

达到这个目的的步骤是:

1- 初始化 NSUndoManager。

2- 使用指定的函数调用在 NSUndoManager 对象中注册对象状态,稍后将针对您的情况进行讨论。

3-在 NSUndoManager 对象中使用撤消或重做或清除操作功能。

Ex 来 self 的工作解决方案

-在.h文件中

@property (nonatomic,retain) NSUndoManager *undoManager;
  • 在.m文件中

    @synthesize undoManager;

  • 在“viewDidLoad”方法中——初始化您的 NSUndoManager

    undoManager = [[NSUndoManager alloc] init];

假设您在类里面有一个使用捏合来放大/缩小的函数,那么在您的“viewDidLoad”中您将拥有

UIPinchGestureRecognizer *pinchGesture =
[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinchGesture.delegate = (id)self;
[self.view addGestureRecognizer:pinchGesture];

所以捏会放大/缩小这样请注意,“MyImageView”是我们要放大/缩小的图像

- (void)pinch:(UIPinchGestureRecognizer*)recognizer{

if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateChanged) {
NSLog(@"gesture.scale = %f", recognizer.scale);

CGFloat currentScale = self.MyImageView.frame.size.width / self.MyImageView.bounds.size.width;
CGFloat newScale = currentScale * recognizer.scale;
//Here is the line that register image to NSUndoManager before making and adjustments to the image "save current image before changing the transformation"
//Add image function is function that fired when you Make undo action using NSUndoManager "and so we maintain only image transformation that changed when you zoom in/out"
[[undoManager prepareWithInvocationTarget:self] AddImage:self.MyImageView.transform];

if (newScale < 0.5) {
newScale = 0.5;
}
if (newScale > 5) {
newScale = 5;
}

CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
self.MyImageView.transform = transform;
recognizer.scale = 1;
}
}

-AddImage 函数将当前图像转换状态保存在 NSUndoManager 中。

-(void)AddImage:(CGAffineTransform)sender{
CGAffineTransform transform = sender;
self.MyImageView.transform = transform;
}

所以如果你有一个按钮可以撤销操作

-(IBAction)OptionsBtn:(id)sender{
if ([undoManager canUndo]) {
[undoManager undo];
}
}

所以如果你想取消所有的操作你有两种方式

while ([undoManager canUndo]) {
[undoManager undo];
}

[undoManager removeAllActions];

关于ios - 使用 CGLayer 撤消重做问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11394839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com