在iOS中为paint应用程序制作橡皮擦工具

前端之家收集整理的这篇文章主要介绍了在iOS中为paint应用程序制作橡皮擦工具前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个绘图应用程序,我想知道如何实现橡皮擦工具.我不想让我的橡皮擦工具绘制白色,因为我想让用户更改背景颜色.而且,是否可以设定刷子的硬度?如果是,请告诉我如何.

谢谢

这是我到目前为止所做的:

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

所以代码将是这样的东西:

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

猜你在找的iOS相关文章