Search code examples
iphonedrawquartz-2dlines

Draw multiple lines Quartz 2D


I want to draw 15 horizontal lines, with 20 points space between them. I can't understand why this code doesn't work.

 - (void)drawRect:(CGRect)rect
 {
  // Drawing code
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
  CGContextSetLineWidth(context, 2.0);
  for (int i=20;i+=20;i<=20*15) {
      CGContextBeginPath(context);
      CGContextMoveToPoint(context, 10, i);
      CGContextAddLineToPoint(context, 310, i);
      CGContextStrokePath(context);

   }

}

Thank you!


Solution

  • Yes, the for loop should be:

    for (int i=20; i<=20*15; i+=20) {
        ...
    }