IOS:绘制一条删除另一条线的线

Cra*_*Dev 0 xcode drawing ios

在我的代码中:

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

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

UIGraphicsBeginImageContext(drawImage.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, a);
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;
}
}
Run Code Online (Sandbox Code Playgroud)

我能够在drawImage中绘制一条线,但是如果我想绘制一条线来移除另一条线?作为取消其他画线的橡皮擦.可能吗?

Cla*_*fou 10

如果要通过绘制用透明度替换下面的线来"擦除"像素,则需要使笔触颜色完全透明,但也需要将混合模式更改为kCGBlendModeCopy(使用默认混合模式,使用完全透明的笔触颜色绘制当然没有效果).

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
Run Code Online (Sandbox Code Playgroud)

在那之后,你就可以绘制"橡皮擦"行,就像你在你的代码片段做(CGContextMoveToPoint,CGContextAddLineToPoint,CGContextStrokePath)