当我绘制新线时,旧线消失

时间:2012-03-28 12:48:34

标签: objective-c core-graphics

我是Objective-C编程的新手。

我的问题是用touchesMoved绘制线条。

我的代码是这样的:

- (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];
}

1 个答案:

答案 0 :(得分:1)

这就是drawRect:的工作方式。

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

还要考虑查看列出其他可能性的question

相关问题