如何用手指擦除部分UIImageView?

时间:2015-07-12 05:07:24

标签: ios objective-c iphone uiimageview cgcontext

基本上我只想制作一个橡皮擦,就像在Paint中一样。我想用手指擦除UIImageView的部分内容。我使用下面的代码,然而,它只用小笔划擦除,而我希望它擦除,直到我拉开手指:

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

}

我应该如何更改我的代码?

0 个答案:

没有答案