如何使用按钮单击擦除绘图?

Ios*_*per 6 ios

我在IOS中绘制应用程序时遇到了一些麻烦.我在一些教程的帮助下创建了自由手绘图.但我发现擦除图纸有些困难.在我的应用程序中,我有橡皮擦按钮作为背景图像.单击橡皮擦按钮后,当我在绘图上滑动时,无论我在哪里滑动,它都会擦除绘图.任何人都可以帮我这样做.提前致谢.

以下是我的代码:

@implementation LinearInterpView
{
    UIBezierPath *path;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {

    if(self = [super initWithCoder:aDecoder]) {
        [self setMultipleTouchEnabled:YES];
        [self setBackgroundColor:[UIColor whiteColor]];
        path=[UIBezierPath bezierPath];
        [path setLineWidth:2.0];
    }
    return self;
}

-(void)drawRect:(CGRect)rect{
    [[UIColor blackColor] setStroke];
    [path stroke];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    CGPoint p=[touch locationInView:self];
    [path moveToPoint:p];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    CGPoint p=[touch locationInView:self];
    [path addLineToPoint:p];
    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesMoved:touches withEvent:event];
}

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

    [self touchesEnded:touches withEvent:event];
}

// This is the button action to erase the drawing.
- (IBAction)erase:(id)sender {
    CGContextRef cgref=UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(cgref, kCGBlendModeClear);
}
Run Code Online (Sandbox Code Playgroud)

请清楚我,我做了什么错.

azm*_*hak 0

为什么不在橡皮擦按钮中实现与绘图按钮中相同的逻辑。只需将橡皮擦中笔划的默认颜色设置为白色或背景颜色即可。