【问题标题】:Clear context graphics清晰的上下文图形
【发布时间】:2013-08-30 00:35:37
【问题描述】:

我正在尝试清除图形上下文。使用这两种方式可以完美清除。

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

【问题讨论】:

  • 你是清除路径还是添加路径?
  • 在清除视图之前,您必须使视图的整个边界无效:[self setNeedsDisplay].
  • @NikolaiRuhe 我已经调用了 [self SetNeedsDisplay],它调用了我的视图的 drawrect。较旧的线路仍在产生问题。

标签: iphone ios core-graphics


【解决方案1】:

您似乎没有在清除整个视图的方法中清除path (eraseButton ?)。

因此路径仍将包含整个旧绘图。只有那些部分会显示您调用setNeedsDisplayInRect:(来自touchesMoved:)。您可以通过重新绘制旧图来显示它。

你应该通过在eraseButton:中创建一个新的来清除path(可能是一个ivar)

CGPathRelease(path);
path = CGPathCreateMutable();

【讨论】:

    猜你喜欢
    • 2021-12-06
    • 2011-08-16
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多