我尝试在ipad上创建一个简单的手指画应用程序。我能够正确地绘制到屏幕的路径,但我希望有一个选项,可以完全清除屏幕上所有绘制的路径。我当前拥有的代码清除了上下文,但是当我调用绘图代码块时,所有的路径都会重新出现在屏幕上。
- (void)drawRect:(CGRect)rect
{
//Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
if(clearContext == 0){
CGContextSetLineWidth(context, 6);
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
CGContextAddPath(context, drawingPath);
CGContextStrokePath(context);
}
if(clearContext == 1){
//This is the code I currently have to clear the context, it is clearly wrong
//Just used as experimentation.
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextAddPath(context, drawingPath);
CGContextStrokePath(context);
clearContext = 0;
}
}发布于 2012-08-29 02:04:49
我假设drawingPath是一个CGPath ..。你要清除那个CGPath吗?这可能就是你问题的原因。
在清除路径的操作中,请执行以下操作:
CGPathRelease(drawingPath);
drawingPath = CGPathCreateMutable();
[self setNeedsDisplay];然后做你通常做的普通绘画。如果路径为空,则不会绘制任何内容
发布于 2020-07-20 05:14:20
清除所有CGContextRef图形:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.bounds); //self.bounds = your drawing base frame
[self setNeedsDisplay];https://stackoverflow.com/questions/12164854
复制相似问题