簡體   English   中英

手指觸摸即可清除UIView的背景色

[英]Clear UIView's BackGround Color on Touch With Finger

我制作了一個圖像編輯應用程序。 所以我想由用戶填充uiview的顏色,然后用戶用手指觸摸擦除顏色。 我為用手指清除UIImageView編寫此代碼,但是如何清除uiview顏色。

我想要制作像PicsArt的Paper Effect。(PicsArt在App Store上)請幫助我...

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

}

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

@米蘭帕特爾。 您的代碼副本帶有一些更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM