0

I'm trying to draw a hexagon in a UIView. I do this by overriding the drawRect method in my subclass of UIView. But when the view is shown I only see the backgroundColor of the view but I don't see my shape being drawn in it.

This is the code in my -drawRect method

CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColor(context, CGColorGetComponents([_fillColor CGColor]));

    NSArray *points = _hex.points;

    CGPoint point = [points[0] CGPointValue];
    CGContextMoveToPoint(context, point.x, point.y);

    for (int i = 1; i < [points count]; i++){
        CGContextAddLineToPoint(context, point.x, point.y);
    }


    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFill);

The array that is being drawn is the following:

  NSMutableArray *mutablePoints = [NSMutableArray arrayWithCapacity:6];
    [mutablePoints addObject:[NSValue valueWithCGPoint:CGPointMake(40, 0)]];
    [mutablePoints addObject:[NSValue valueWithCGPoint:CGPointMake(59, 0)]];
    [mutablePoints addObject:[NSValue valueWithCGPoint:CGPointMake(68.5, 16.4544)]];
    [mutablePoints addObject:[NSValue valueWithCGPoint:CGPointMake(59, 32.9089)]];
    [mutablePoints addObject:[NSValue valueWithCGPoint:CGPointMake(40, 32.9089)]];
    [mutablePoints addObject:[NSValue valueWithCGPoint:CGPointMake(30.5, 16.4544)]];
wvp
  • 1,134
  • 5
  • 15
  • 28

2 Answers2

0

Edit : you forgot online in for loop look this code

  for (int i = 1; i < [points count]; i++) {

        point = [points[i] CGPointValue]; // YOU FORGOT !

        CGContextAddLineToPoint(context, point.x, point.y);
    }
Jasmin
  • 794
  • 7
  • 18
0

You don't change the point in yoour loop ! Just add:

point = [points[i] CGPointValue]

inside your loop ?

Pierre Marty
  • 166
  • 1
  • 7