1

I have an array of CGpoint and I want to connect these points to create a line. I know how to do it by subclass the UIView. but what I want to do here is that I already have an array of CGPoint and I have a button, when I click the button the line will be shown.

I don't know how to do this.

However I try something like following: (aLine is the array with CGPoint)

#define POINT(X) [[aLine objectAtIndex:X]CGPointValue]


CGContextRef context = UIGraphicsGetCurrentContext();
for (int i = 0;i < (aLine.count-1);i++){
     CGPoint pt1 = POINT(i);
     CGPoint pt1 = POINT(i+1);
     CGContextMoveToPoint(context,pt1.x,pt1.y);
     CGContextAddLineToPoint(context,pt2.x,pt2.y);
     CGContextStrokePath(context);
}

Anyone can help me? thanks.

Dawson
  • 73
  • 1
  • 8

1 Answers1

0

You're almost there! Firstly you need to set the stroke colour...

CGContextSetStrokeColor(context, CGColorGetComponents([colour CGColor]));

Then when you're done connecting your points (exactly what your code is currently doing) just close the path and stroke it...

CGContextClosePath(context);
CGContextDrawPath(context, kCGPathStroke);
mmccomb
  • 13,516
  • 5
  • 39
  • 46