0

我终于设法得到了一直没有清除的 DrawRect (我也有 setClearsContextBeforeDrawing:YES )

- (void)drawRect:(CGRect)rect
{
    UIGraphicsPushContext(drawingContext);
    CGImageRef cgImage = CGBitmapContextCreateImage(drawingContext); 
    UIImage *uiImage = [[UIImage alloc] initWithCGImage:cgImage];

    CGContextSetLineWidth(drawingContext, 4.0);
    if (draw)
        CGContextSetStrokeColorWithColor(drawingContext, [UIColor  whiteColor] CGColor]);
    else
        CGContextSetStrokeColorWithColor(drawingContext, [[UIColor clearColor] CGColor]);

    CGContextMoveToPoint(drawingContext,    lastPt.x - (31.0 / self.transform.a),  lastPt.y - (31.0 / self.transform.a)  );
    CGContextAddLineToPoint(drawingContext, currPt.x - (31.0 / self.transform.a),  currPt.y - (31.0 / self.transform.a)  );
    CGContextStrokePath(drawingContext);

    UIGraphicsPopContext();
    CGImageRelease(cgImage);
    [uiImage drawInRect: rect];

    lastPt = currPt;
}

这会在绘制时留下一条线
我的问题是当绘制为 NO [UIColor clearColor] 未擦除时我应该用什么来擦除?提前致谢

4

1 回答 1

2

因为clearColoralpha 为零,所以用它绘制通常没有任何效果。您需要将上下文的混合模式设置kCGBlendModeCopy为使其生效。

但是,将上下文的混合模式设置为kCGBlendModeClear您想要擦除的时间并返回到kCGBlendModeNormal您想要绘制的时间会更简单:

if (draw) {
    CGContextSetBlendMode(drawingContext, kCGBlendModeNormal);
    CGContextSetStrokeColorWithColor(drawingContext, [UIColor  whiteColor] CGColor]);
} else {
    CGContextSetBlendMode(drawingContext, kCGBlendModeClear);
}
于 2012-06-24T19:25:50.083 回答