2012-03-28 37 views
0

我是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

這是怎麼drawRect:應該工作。

您應該將所有內容繪製到屏幕外的緩衝區中(例如CGImageCGLayer),然後使用drawRect:來繪製緩衝區。

也考慮到這question其中列出了其他可能性。