我正在尝试清除图形上下文。它使用这两种方式完美地清除。
1. clearContextBeforeDrawing
2. CGContextClearRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, rect);
CGContextFlush(context);
但是当我再次画线时。前面的行与出现点的新行混合在一起。有人可以帮忙吗?
这就是我清除线条的方式
-(IBAction)eraseButton:(id)sender
{
self.viewmainHandwriting.isErase = TRUE;
[self.viewmainHandwriting setBackgroundColor:[UIColor clearColor]];
[self.viewmainHandwriting setNeedsDisplay]; //It calls my drawRect Function
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
if (!self.isErase)
{
CGContextAddPath(context, path);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextStrokePath(context);
self.empty = NO;
}
else
{
CGContextClearRect(context, self.bounds);
path
self.isErase = FALSE;
}
}
我是如何绘制线条
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
previousPoint1 = [touch previousLocationInView:self];
previousPoint2 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];
[self touchesMoved:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
/* check if the point is farther than min dist from previous */
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];
}
答案 0 :(得分:1)
在清除整个视图(path
?)的方法中,您似乎没有清除eraseButton
。
所以路径仍然包含整个旧图。只显示您调用setNeedsDisplayInRect:
的部分(来自touchesMoved:
内)。您可以通过再次绘制来显示旧图纸。
您应该在path
eraseButton:
(可能是ivar)
CGPathRelease(path);
path = CGPathCreateMutable();