How to remove part of UIImageView with finger?

Basically, I just want to create an eraser like in Paint

. I want to remove parts with UIImageView

just my finger. I am using the following code, however it only erases with small strokes, while I want it to erase until I pull my finger away:

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

    UITouch *touch = [touches anyObject];
    lastTouch = [touch locationInView:bg];
}

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


    CGFloat brushSize = 35;

    CGColorRef strokeColor = [UIColor clearColor].CGColor;

    UITouch *touch = [touches anyObject];
    currentTouch = [touch locationInView:bg];

    UIGraphicsBeginImageContext(bg.frame.size);
    CGContextRef context2 = UIGraphicsGetCurrentContext();
    [bg.image drawInRect:CGRectMake(0, 0, bg.frame.size.width, bg.frame.size.height)];
    CGContextSetLineCap(context2, kCGLineCapRound);

    CGContextSetLineWidth(context2, brushSize);
    CGContextSetStrokeColorWithColor(context2, strokeColor);
    CGContextSetBlendMode(context2, kCGBlendModeClear);
    CGContextBeginPath(context2);
    CGContextMoveToPoint(context2, lastTouch.x, lastTouch.y);
    CGContextAddLineToPoint(context2, currentTouch.x, currentTouch.y);
    CGContextStrokePath(context2);
    bg.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastTouch = currentTouch;

}

      

How can I change the code?

+3


source to share





All Articles