Objective c 如何在Xcode中为该行创建橡皮擦?

Objective c 如何在Xcode中为该行创建橡皮擦?,objective-c,ios,xcode,line,Objective C,Ios,Xcode,Line,怎么,我有这个密码。当触摸移动时,视图会添加一条线。 现在,如果我想为这行创建一个橡皮擦,我该怎么做? 请早点回答我 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:drawView]; UIGraphicsBeg

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

- (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;   
}

我想这就是你想要的:


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

  • 第一个选项是使用具有相同背景颜色的笔刷 背景视图,使其感觉为线被删除,同时 实际上,刚刚用与背景相同的颜色完成了绘制

  • 第二个选项是使用具有清晰颜色的笔刷并设置 “混合模式”以清除,因此它会删除线条,并且背景视图仍处于空白状态 看得见

    如果(iSeries) {

    }


  • 虽然这在理论上可以回答这个问题,但请您在回答中包含链接文章的基本部分,并提供链接以供参考。如果没有做到这一点,答案将面临链接失败的风险。您好,非常感谢您的回答!我已经像这样插入了你的代码:如果我有一个图像视图,上面有一个图像,上面已经画了一些图形。现在,如果我使用此代码删除图形,它将删除图形以及显示背景视图的图像。此链接可帮助您
    CGContextSetLineWidth(currentContext, 10);
    
    CGContextSetStrokeColorWithColor(currentContext, [UIColor clearColor].CGColor);
    
    CGContextSetFillColorWithColor(currentContext, [UIColor clearColor].CGColor);
    
    CGContextSetBlendMode(currentContext, kCGBlendModeClear);
    
    CGContextDrawPath(currentContext, kCGPathStroke);