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). 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.