我目前正在使用 drawRect 方法来允许用户用手指绘画。我使用 kCGBlendModeClear 清除部分绘画。但是,当我开始使用 kCGBlendModeNormal 再次绘制时,如果我将手指移到曾经被擦除的区域上,它会将其绘制到视图中。很奇怪。
我已经查看了所有内容并尝试了很多不同的建议,但我无法得到任何工作。有谁能帮忙吗?
感动中:
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);