2012-06-01 117 views
0

以下是我用来释放手绘的代码。但每当我改变画笔的颜色时,新的颜色也会应用到之前的曲线。这是为什么发生。更改贝塞尔曲线的颜色

- (void)drawRect:(CGRect)rect 
{ 

for (UIBezierPath *_path in pathArray) { 

    [brushPattern setStroke]; 

    _path.lineCapStyle = kCGLineCapRound; 

    [_path stroke]; 

    } 
} 


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

isEdited=YES; 

myPath=[[UIBezierPath alloc]init]; 

myPath.lineWidth=lineWidths; 

CGPoint touchPoint = [[touches anyObject] locationInView:self]; 

UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 

[myPath moveToPoint:[mytouch locationInView:self]]; 

[myPath addLineToPoint:CGPointMake(touchPoint.x+1, touchPoint.y+1)]; 

[pathArray addObject:myPath]; 

[self setNeedsDisplay]; 


} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 

[myPath addLineToPoint:[mytouch locationInView:self]]; 

[self setNeedsDisplay]; 


} 

回答

1

因为您在drawRect中重绘所有路径,所有路径都使用相同的颜色(brushPattern)。如果您有不同颜色的路径,则必须在绘制时存储正在使用的颜色,并将其设置为绘图循环中的笔触颜色。

我建议你的pathArray拥有字典,每个字典有一个路径和一个颜色,而不是只保存路径。

+0

我添加了NSMutableDictionary并添加了[dict setObject:brushPattern forKey:myPath]; [pathArray addObject:dict];在接触开始。我怎样才能在drawrect中迭代? –

+0

我确实在字典中添加了颜色和路径,并且在字典中添加了字典。我正在获取该数组中的不同路径和颜色,但是当我绘制新路径时,即使不更改颜色,以前的路径也会消失。任何人都可以告诉我为什么会这样? –

+0

你必须包含你的代码,也许在一个新的问题。 – jrturton