【问题标题】:how to undo the line draw using CGContextAddLineToPoint?如何使用 CGContextAddLineToPoint 撤消画线?
【发布时间】:2014-10-08 07:25:29
【问题描述】:

我正在为手绘开发一个画笔应用程序,我是核心图形框架的新手。

现在我的应用程序准备好仅使用以下代码进行绘图

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!)
{
    println("Enter in touchesBegan method");
    mouseSwiped = false;
    var touch : UITouch = touches.anyObject() as UITouch;
    lastPoint = touch.locationInView(self.view);
}

 override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!)
{
    println("Enter in touchesMoved method");

    mouseSwiped = true;
    var touch : UITouch = touches.anyObject() as UITouch;
    var currentPoint : CGPoint = touch.locationInView(self.view);
    UIGraphicsBeginImageContext(self.view.frame.size);

    self.tempDrawImage!.image?.drawInRect(CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height));

    println("lastPoint.X :\(lastPoint!.x) , lastPoint.Y :\(lastPoint!.y)")
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint!.x, lastPoint!.y);
    println("currentPoint.X :\(currentPoint.x) , currentPoint.Y :\(currentPoint.y)")
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red!, green!, blue!, 1.0)
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush!);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    //contexts!.addObject(UIGraphicsGetCurrentContext());


    self.tempDrawImage!.image = UIGraphicsGetImageFromCurrentImageContext();
    tempDrawImage!.alpha = opacity!;
    UIGraphicsEndImageContext();
    println("dot");
    lastPoint = currentPoint;
}

 override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!)
{
    println("Enter in touchesEnded method");

    if mouseSwiped == false
    {
        println("false");
        UIGraphicsBeginImageContext(self.view.frame.size);

        self.tempDrawImage!.image?.drawInRect(CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height));
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush!);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red!, green!, blue!, opacity!);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint!.x, lastPoint!.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint!.x, lastPoint!.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext())
        CGContextFlush(UIGraphicsGetCurrentContext());

        self.tempDrawImage!.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

    UIGraphicsBeginImageContext(self.mainImage!.frame.size);

    self.mainImage!.image?.drawInRect(CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height ), blendMode: kCGBlendModeNormal, alpha: 1.0);

    self.tempDrawImage!.image.drawInRect(CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height), blendMode: kCGBlendModeNormal, alpha: opacity!);

    self.mainImage!.image = UIGraphicsGetImageFromCurrentImageContext();

    self.tempDrawImage!.image = nil

    UIGraphicsEndImageContext();
}

但在某些应用程序中,我看到它们在其应用程序中提供撤消功能,因此我也想提供此功能,如果使用绘制 2 行并按下撤消按钮,而不是最后添加的行从图像视图中一一删除

我对此进行了搜索,但我无法理解,并且没有一个答案被接受,所以我很困惑,所以请帮助我。

谢谢!

【问题讨论】:

    标签: ios core-graphics cgcontext


    【解决方案1】:

    在您的情况下,因为您只添加行,这变得非常简单。您需要做的就是创建一个可变数组,该数组将包含来自用户输入的所有点。

    现在,在每次触摸开始或最终将使用的任何内容时,您都将从代码中已有的点创建一个NSValue,并将此值添加到我之前提到的数组中。添加该行之后,您需要做的就是调用一些“刷新”方法,或者使用setNeedsDisplay 告诉视图重新显示,这将触发调用draw rect 方法。其余代码将从该方法中移出...

    现在从撤消按钮的目标中,您将从数组中删除最后两个点(我希望这里没有问题)并调用刷新或重新显示(与触摸事件相同)。

    因此,无论是将绘图代码移至“刷新”方法还是移至“绘制矩形”,您现在需要做的是首先清除上下文背景,然后遍历您的点数组并绘制每条线.

    够简单吗?

    【讨论】:

    • 如果您绘制的点数是奇数,则跳过最后一个。此外,如果计数为奇数,则在按撤消时,您可能希望删除 3 个点,如果计数小于 2,则应禁用撤消选项。
    • 我将哪两点添加到数组以及如何重绘我无法通过,因为我是新手,这是我第一次接触图形,所以我不明白。对不起!
    • 您每次触摸添加一个点,即使您将其命名为 currentPoint。然后,当您绘制时,您会像 for(int i=0; i
    • 但我如何从 imageView 中删除无法理解的理论点
    • 嗨,我应用了你的答案,但是由于使用循环重绘,这项工作非常缓慢,所以请你给点建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2011-04-26
    • 2021-06-01
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多