kCGBlendModeClear擦除但然后重新出现行

时间:2013-09-14 18:28:50

标签: ios objective-c paint drawrect

我目前正在使用drawRect方法让用户用手指画画。我使用kCGBlendModeClear来清除部分绘画。但是,当我开始使用kCGBlendModeNormal再次绘制时,如果我将手指移动到曾经被擦除的区域上,它会将其绘制成视图。很奇怪。

我看了很多,并尝试了很多不同的建议,但我无法得到任何工作。有人能帮忙吗?

In Touches Moved:

UITouch *touch = [touches anyObject];

CGPoint point = [touch locationInView:self];

CGFloat dx = point.x - currentPoint.x;
CGFloat dy = point.y - currentPoint.y;

if ((dx * dx + dy * dy) < kPointMinDistanceSquared) {
    return;
}


previousPoint2 = previousPoint1;
previousPoint1 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];

CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGMutablePathRef subpath = CGPathCreateMutable();
CGPathMoveToPoint(subpath, NULL, mid1.x, mid1.y);
CGPathAddQuadCurveToPoint(subpath, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGRect bounds = CGPathGetBoundingBox(subpath);

CGPathAddPath(path, NULL, subpath);
CGPathRelease(subpath);

CGRect drawBox = bounds;
drawBox.origin.x -= self.lineWidth * 2.0;
drawBox.origin.y -= self.lineWidth * 2.0;
drawBox.size.width += self.lineWidth * 4.0;
drawBox.size.height += self.lineWidth * 4.0;

[self setNeedsDisplayInRect:drawBox];

在drawRect:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddPath(context, path);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 10);

if (eraserSelected) {

     //////Erase/////// 
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextSetLineWidth(context, 37.0);

}else {

    //////Paint///////
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetAlpha (context, 0.75);


}


CGContextStrokePath(context);

1 个答案:

答案 0 :(得分:0)

问题是你只有一行。每次触摸点移动时都会更新该单行,并在触摸点周围重绘该区域。重绘区域使用当前的“工具”(橡皮擦或笔)。因此,当用户绘制任何线段时,橡皮擦/笔的状态不会被保存。

要解决此问题,您需要创建一个新行,至少每次用户更改“工具”时(可能每次他们结束触摸会话时)。该行应使用当时处于活动状态的“工具”类型和订单号保存(因此,当您来到drawRect时,可以按顺序绘制它们。)