2014-12-05 43 views
0

我製作了一個圖像編輯應用程序。所以我想用戶填寫uiview的顏色,用戶用手指觸摸擦除顏色。 我使用手指清除UIImageView的代碼,但是如何清除uiview的顏色。用手指清除UIView的背景色

我要讓喜歡PicsArt不僅的紙效果。(PicsArt不僅在App商店) 請幫我...

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

}

回答

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

@Milan帕特爾。你有一些更新代碼的副本。

+0

我想清除視圖的背景顏色..請幫助..如何畫這個.. – 2014-12-06 05:44:42

+0

[[UIColor clearColor] setFill]; CGContextFillRect(context,rect); – x4snowman 2014-12-06 07:10:13

+0

在您繼續編寫代碼之前,您可以參考Quartz 2D Programming Guide,它可以幫助您瞭解繪圖系統。 https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/Introduction.html – x4snowman 2014-12-06 07:11:40