【问题标题】:iphone:How to redo/undo while drawing through free handiphone:如何在通过徒手绘图时重做/撤消
【发布时间】:2012-03-08 07:23:16
【问题描述】:

实际上,我通过徒手绘制图像的示例很少,并且我也集成到了我的应用程序中。我需要其他功能,例如撤消/重做。我怎样才能实现这一点?在这方面需要帮助..我使用了以下代码。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y -= 20;


    UIGraphicsBeginImageContext(self.view.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

    mouseMoved++;

    if (mouseMoved == 10) {
        mouseMoved = 0;
    }
}

【问题讨论】:

标签: iphone cgcontext uitouch


【解决方案1】:
    CGMutablePathRef path;
    path = CGPathCreateMutable();
    - (void)drawRect:(CGRect)rect
    {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGPathMoveToPoint(path, NULL, previousPoint.x, previousPoint.y);
    CGPathAddLineToPoint(path, NULL, lastPoint.x, lastPoint.y);
    CGContextAddPath(context, path);

    CGContextSetLineWidth(context, 10.0);
[[UIColor greenColor] setStroke];
    CGContextDrawPath(context,kCGPathFillStroke);
    }

现在您可以通过路径的索引执行撤消/重做。

【讨论】:

  • 我怎样才能得到previousPoin ??在你的例子中??
【解决方案2】:

您可以尝试将所有路径存储在一个数组中。 当你画一个新的,最后添加它并绘制它,当你删除 - 删除最后一个并重新绘制所有。在另一个数组中保留删除,以便您可以重做操作,在将新路径添加到路径数组时清除重做数组。 这是我能想到的最直接的方法。

【讨论】:

  • 谢谢。如果能给我样品就更好了。
  • 您需要澄清一下吗?将您的路径创建为CGMutablePathRef,您可以将它们存储在一个数组中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-03
  • 1970-01-01
  • 2012-03-20
  • 2010-12-06
  • 2023-01-07
相关资源
最近更新 更多