简体   繁体   English

更改贝塞尔曲线的颜色

[英]Changing the color of the bezier curve

Following is the code am using to free hand drawing. 以下是用于释放手绘图的代码。 But every time i change the color of the brush, new color is applied to the previous curves also. 但是每次我更改画笔的颜色时,新的颜色也会应用于以前的曲线。 Why is this happening. 为什么会这样呢?

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


}

Because you're redrawing all of your paths in drawRect, all with the same colour (brushPattern). 因为您要重新绘制drawRect中的所有路径,所以所有路径都具有相同的颜色(brushPattern)。 If you have paths of different colours, you have to store the colour in use at the time of drawing and set that as your stroke colour in your drawing loop. 如果您使用不同颜色的路径,则必须存储绘图时使用的颜色,并将其设置为绘图循环中的笔触颜色。

I'd suggest that your pathArray holds dictionaries, each dictionary having a path and a colour, instead of just holding paths. 我建议您的pathArray包含字典,每个字典都具有路径和颜色,而不是仅包含路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM