如何实现绘图中的擦除功能,使用户可以清除所有的绘图



我使用CoreGraphics在背景图像上实现自由手绘,这对我来说很好,现在我想为这个绘图实现擦除功能,以便用户可以清除他的绘图

如果你只想清除屏幕,删除你创建的CGPaths,它将不会绘制任何内容。

如果你想让使用切换到橡皮擦工具,只擦除某些东西,你需要设置一个新的绘图类,让你在你的其他图纸上绘制背景或调整类,你必须不绘制原始路径和橡皮擦路径之间的交叉点。

你可以在UITouch方法"touchesMoved"中使用它来清除你绘制的路径

' UITouch *touch = [[event alltouch] anyObject];

currenttouch = [touch locationInView:drawingImageView];

CGColorRef strokeColor = [UIColor blackColor].CGColor;
UIGraphicsBeginImageContext(drawingImageView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    [drawingImageView.image drawInRect:CGRectMake(0, 0, drawingImageView.frame.size.width, drawingImageView.frame.size.height)];
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, brushSize);
    CGContextSetStrokeColorWithColor(context, strokeColor);
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, currenttouch.x, currenttouch.y);
    CGContextAddLineToPoint(context, currenttouch.x, currenttouch.y);
    CGContextStrokePath(context);
    drawingImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

"

最新更新