改变贝塞尔曲线的颜色

时间:2012-06-01 06:08:59

标签: iphone objective-c ios cocoa-touch bezier

以下是用于免费手绘的代码。但每次我更改画笔的颜色时,新颜色也会应用于之前的曲线。为什么会这样。

    - (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 个答案:

答案 0 :(得分:1)

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

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