I at last managed to get DrawRect that did not clear all the time using
(I also have the 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;
}
This leaves behind a line at it is drawn
My problem is when draw is NO [UIColor clearColor] is NOT erasing What am I supposed to use to erase ? Thanks in advance
Because
clearColorhas an alpha of zero, drawing with it normally doesn’t have any effect. You need to set the context’s blend mode tokCGBlendModeCopyto make it take effect.However, it would be simpler to just set the context’s blend mode to
kCGBlendModeClearwhen you want to erase, and back tokCGBlendModeNormalwhen you want to draw: