Objective c 当我画新线时,旧线消失了

Objective c 当我画新线时,旧线消失了,objective-c,core-graphics,Objective C,Core Graphics,我不熟悉Objective-C编程 我的问题是用触摸画线 我的代码如下: - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetShouldAntialias(context, YES); CGContextSetLineWidth(context, 7.0f); CGContextSetRGBStrokeColor(

我不熟悉Objective-C编程

我的问题是用触摸画线

我的代码如下:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetShouldAntialias(context, YES);

    CGContextSetLineWidth(context, 7.0f);

    CGContextSetRGBStrokeColor(context, 0.7, 0.7, 0.7, 1.0);

    CGContextMoveToPoint(context, self.touchedPoint2.x, self.touchedPoint2.y);

    CGContextAddLineToPoint(context, self.touchedPoint.x, self.touchedPoint.y); 

    CGContextDrawPath(context,kCGPathFillStroke);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.touchedPoint = [[touches anyObject] locationInView:self];

    [self setNeedsDisplay];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   self.touchedPoint2 = [[touches anyObject] locationInView:self];
}

这就是
drawRect:
的工作原理

您应该将所有内容绘制到屏幕外缓冲区(例如
CGImage
CGLayer
),然后仅使用
drawRect:
绘制缓冲区

还可以考虑查看这个列表,其中列出了其他可能性