用手指触摸清除UIView BackGround颜色

时间:2014-12-05 12:56:29

标签: ios uiview uiimageview uigraphicscontext

我制作了一个图像编辑应用程序。所以我想用户填写uiview的颜色,用户用手指触摸擦除颜色。 我用手指编写了UIImageView clear的代码,但是如何清除uiview颜色。

我想像PicsArt的Paper Effect那样。(App Store上的PicsArt) 请帮帮我......

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    lastTouch = [touch locationInView:_imageview];}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    CGPoint currentTouch = [touch locationInView:_imageview];
    UIGraphicsBeginImageContext(_imageview.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [_imageview.image drawInRect:CGRectMake(0, 0, _imageview.frame.size.width, _imageview.frame.size.width)];
    CGContextSetLineCap(context, kCGLineCapRound);
   CGContextSetLineWidth(context,10);
   CGContextSetBlendMode(context, kCGBlendModeClear);
   CGContextBeginPath(context);
   CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
   CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
   CGContextStrokePath(context);
  _imageview.image= UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  lastTouch = [touch locationInView:_imageview];}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{


 UITouch *touch = [touches anyObject];
CGPoint currentTouch = [touch locationInView:_imageview];
UIGraphicsBeginImageContext(_imageview.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[_imageview.image drawInRect:CGRectMake(0, 0, _imageview.frame.size.width, _imageview.frame.size.width)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context,10);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
CGContextStrokePath(context);
_imageview.image= UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

lastTouch = [touch locationInView:_imageview];

}

1 个答案:

答案 0 :(得分:0)

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    lastTouch = [touch locationInView:_imageview];}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    CGPoint currentTouch = [touch locationInView:_imageview];


    lastTouch = [touch locationInView:_imageview];
    //redraw the view
    [self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{


    lastTouch = [touch locationInView:_imageview];
    //redraw your view
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
//Put your draw code here;

//    UIGraphicsBeginImageContext(_imageview.frame.size);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    [_imageview.image drawInRect:CGRectMake(0, 0, _imageview.frame.size.width, _imageview.frame.size.width)];
//    CGContextSetLineCap(context, kCGLineCapRound);
//    CGContextSetLineWidth(context,10);
//    CGContextSetBlendMode(context, kCGBlendModeClear);
//    CGContextBeginPath(context);
//    CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
//    CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
//    CGContextStrokePath(context);
//    _imageview.image= UIGraphicsGetImageFromCurrentImageContext();
//    
//    UIGraphicsEndImageContext();
}
@Pilan patel。您的代码副本,包含一些更新。