我如何在 Xcode 中为这条线创建橡皮擦?

发布于 2024-12-20 16:44:47 字数 1198 浏览 1 评论 0原文

怎么样,我有这个代码。当触摸移动时,视图会添加一条线。 现在,如果我想为这条线创建一个橡皮擦,我该怎么做? 请早点回答我!

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:drawView];

    UIGraphicsBeginImageContext(drawView.frame.size);
    [drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushDimension);

    const CGFloat *components = CGColorGetComponents([brushColor CGColor]);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]);

    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    drawView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;   
}

How, i've this code. When the touch moved, the view adds a line.
Now, if i want to create an eraser for this line, how can i do?
Please, answer me early!

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:drawView];

    UIGraphicsBeginImageContext(drawView.frame.size);
    [drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushDimension);

    const CGFloat *components = CGColorGetComponents([brushColor CGColor]);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]);

    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    drawView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身情人 2024-12-27 16:44:47

如果您正在寻找一种擦除功能,用户可以使用触摸来擦除该行的一部分,而不是 RickyTheCoder 提供的撤消功能,那么您有 2 个选择。

  1. 第一个选项是使用具有相同背景颜色的画笔
    背景视图,因此它认为线条被擦除了
    实际上只是用与背景相同的颜色进行绘制。

  2. 第二个选项是使用清晰颜色的画笔并设置
    混合模式清除,因此它会擦除线条并且背景视图仍然存在
    可见。

    if (isErase)
    {

    CGContextSetLineWidth(currentContext, 10);
    
    CGContextSetStrokeColorWithColor(currentContext, [UIColor clearColor].CGColor);
    
    CGContextSetFillColorWithColor(currentContext, [UIColor clearColor].CGColor);
    
    CGContextSetBlendMode(currentContext, kCGBlendModeClear);
    
    CGContextDrawPath(currentContext, kCGPathStroke);
    

    }

If you are looking for an erase function that the user can use touches to erase portion of the line instead of undo provide by RickyTheCoder, you have 2 options.

  1. The first option is use the brush that has the same background color of
    the background view so it perceive as line got erased while it
    actually just got paint over with the color that is same as the background.

  2. The second option is to use the brush with clear color and set the
    blend mode to clear so it erase the line and the background view is still
    visible.

    if (isErase)
    {

    CGContextSetLineWidth(currentContext, 10);
    
    CGContextSetStrokeColorWithColor(currentContext, [UIColor clearColor].CGColor);
    
    CGContextSetFillColorWithColor(currentContext, [UIColor clearColor].CGColor);
    
    CGContextSetBlendMode(currentContext, kCGBlendModeClear);
    
    CGContextDrawPath(currentContext, kCGPathStroke);
    

    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文