title: How to create two strokes of different color using one UIBezierPath
body: <p>I am trying to draw  lines on a <code>UIView</code> with finger and it worked fine with one color. If I try to change the color and draw again, the previous <code>UIBezierPath</code> color is also changed to new color. So, I am unable to draw a different color line keeping the previous color line on the <code>UIView</code></p>
  
  <p>I set all the properties(path,linecolor) as nonatomic and strong in my <code>UIView</code></p>
  
  <p>For reference:</p>
  
  <p>my first draw:<a href="https://i.stack.imgur.com/jXh0Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jXh0Y.png" alt="enter image description here"></a></p>
  
  <p>my second draw:<a href="https://i.stack.imgur.com/0oO3p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0oO3p.png" alt="enter image description here"></a></p>
  
  <p>my third draw: <a href="https://i.stack.imgur.com/JEsiV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JEsiV.png" alt="enter image description here"></a></p>
  
  <p>After I choose the color, I change the stroke color of my UIView in color picker delegate method:</p>
  
  <pre><code>#pragma mark - FCColorPickerViewControllerDelegate Methods
  
  -(void)colorPickerViewController:(FCColorPickerViewController *)colorPicker didSelectColor:(UIColor *)color {
  
        self.drawView.lineColor = color; //this works fine 
  
   //    self.drawView.path=[UIBezierPath bezierPath];  tried this to create new bezier path with new color, but this erases the olde bezier path and return new
  //    [self.drawView.lineColor setStroke];     tried this
  //    [self.drawView.lineColor setFill];  tried this
  
        [self dismissViewControllerAnimated:YES completion:nil]; //dismiss the color picker
  }
  </code></pre>
  
  <p>Here are my view methods for drawing:</p>
  
  <pre><code>- (id)initWithCoder:(NSCoder *)aDecoder 
  {
      if (self = [super initWithCoder:aDecoder])
      {
          [self setBackgroundColor:[UIColor whiteColor]];
          path = [UIBezierPath bezierPath];
          [path setLineWidth:self.lineWidth];
      }
      return self;
  }
  
  - (void)drawRect:(CGRect)frame 
  {
      [self.lineColor setStroke];
  
      [path stroke];
  
  }
  
  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
      dispatch_async(dispatch_get_main_queue(), ^{
          UITouch *touch = [touches anyObject];
          CGPoint p = [touch locationInView:self];
          [path moveToPoint:p];
      });
  
  }
  
  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  {
     dispatch_async(dispatch_get_main_queue(), ^{
         UITouch *touch = [touches anyObject];
         CGPoint p = [touch locationInView:self];
         [path addLineToPoint:p];
         [self setNeedsDisplay];
     });
  }
  
  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  {
      [self touchesMoved:touches withEvent:event];
  }
  </code></pre>
  
  <p>I tried <a href="https://stackoverflow.com/questions/14903572/uibezierpath-multiple-line-colors">this:</a> adding the old bezier paths to the array and redraw them, but it didnt work , this time I cant make another bezier path with new color.:</p>
  
  <pre><code>       - (void)drawRect:(CGRect)frame // (5)
  {
      //load the path from array
      for (int i = 0; i &lt; [pathArray count]; i++){
  
  
          NSLog(@"Path: %@",[pathArray objectAtIndex:0]);
          NSLog(@"Color: %@",[pathArray objectAtIndex:1]);
  
          UIBezierPath *oldpath = [pathArray objectAtIndex:0];
  
           //color
          [[pathArray objectAtIndex:1] setStroke];
  
          //path
          [oldpath stroke];
      }
  
      UIBezierPath *newPath = [self pathForCurrentLine];
      if (newPath)
      {
          // set the width, color, etc, too, if you want
           [lineColor setStroke];
          [newPath stroke];
      }
  
  }
  
     - (UIBezierPath*)pathForCurrentLine {
      if (CGPointEqualToPoint(startPoint, CGPointZero) &amp;&amp; CGPointEqualToPoint(endPoint, CGPointZero)){
          return nil;
      }
  
      UIBezierPath *newpath = [UIBezierPath bezierPath];
      [newpath moveToPoint:startPoint];
      [newpath addLineToPoint:endPoint];
  
      return newpath;
  
  }
  
  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
      dispatch_async(dispatch_get_main_queue(), ^{
          UITouch *touch = [touches anyObject];
          CGPoint p = [touch locationInView:self];
          [path moveToPoint:p];
          startPoint=p;
      });
  
  }
  
  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  {
     dispatch_async(dispatch_get_main_queue(), ^{
         UITouch *touch = [touches anyObject];
         CGPoint p = [touch locationInView:self];
         [path addLineToPoint:p]; // (4)
         endPoint=p;
  
         [self setNeedsDisplay];
     });
  }
  
  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  {
      [self touchesMoved:touches withEvent:event];
  
      [pathArray addObject:path];
      [pathArray addObject:lineColor];
  
  }
  
  - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  {
      [self touchesEnded:touches withEvent:event];
  }
  </code></pre>