我正在创建一个绘图应用程序,我想知道如何实现橡皮擦工具.我不想让我的橡皮擦工具绘制白色,因为我想让用户更改背景颜色.而且,是否可以设定刷子的硬度?如果是,请告诉我如何.
谢谢
这是我到目前为止所做的:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; lastPoint = [touch locationInView:self.view]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.view]; UIGraphicsBeginImageContext(self.view.frame.size); [drawImage.image drawInRect:CGRectMake(0,self.view.frame.size.width,self.view.frame.size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(),10); CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.0,1.0); CGContextMoveToPoint(UIGraphicsGetCurrentContext(),lastPoint.x,lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),currentPoint.x,currentPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); lastPoint = currentPoint; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.view]; UIGraphicsBeginImageContext(self.view.frame.size); [drawImage.image drawInRect:CGRectMake(0,self.view.frame.size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(),10); CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),1.0); CGContextMoveToPoint(UIGraphicsGetCurrentContext(),lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),currentPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); lastPoint = currentPoint; } - (IBAction)clear:(id)sender { drawImage.image = nil; }
解决方法
好的,这就是我为橡皮擦工具所做的:
我添加这行代码:
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);
我添加这行代码:
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);
所以代码将是这样的东西:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.view]; UIGraphicsBeginImageContext(self.view.frame.size); [drawImage.image drawInRect:CGRectMake(0,self.view.frame.size.height)]; // I add this CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear); CGContextSetLineCap(UIGraphicsGetCurrentContext(),sizeSlider.value); CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear); CGContextMoveToPoint(UIGraphicsGetCurrentContext(),currentPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); lastPoint = currentPoint; }