我试图找到一种在视图上绘制线条而不重绘所有上下文的方法。
这是我的绘图方法:
-(void)drawInContext:(CGContextRef)context {
for (int i = 0; i < self.drawings.count; ++i) {
Drawing* drawing = [self.drawings objectAtIndex:i];
CGContextSetStrokeColorWithColor(context, drawing.colorTrait.CGColor);
CGContextSetLineWidth(context, 1.0);
CGContextMoveToPoint(context, [[drawing.points objectAtIndex:0] CGPointValue].x * self.zoomScale, [[drawing.points objectAtIndex:] CGPointValue].y * self.zoomScale);
for (int i = 1; i < drawing.points.count; i++) {
CGContextAddLineToPoint(context, [[drawing.points objectAtIndex:i] CGPointValue].x * self.zoomScale, [[drawing.points objectAtIndex:i] CGPointValue].y * self.zoomScale);
}
CGContextStrokePath(context);
}
}
-(void)drawRect:(CGRect)rect {
if (isRedrawing) {
[self drawInContext:UIGraphicsGetCurrentContext()];
isRedrawing = NO;
}
[[UIColor redColor] set];
[currentPath stroke];
}
但是当我在我的 touches 方法中调用 setNeedsDisplay 时,视图被完全清除了。有没有办法让我的方法奏效?