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)]];
4

2 に答える 2

0

ループのポイントを変更しないでください。追加するだけです:

ポイント = [ポイント[i] CGPointValue]

あなたのループの中に?

于 2014-07-29T08:54:36.557 に答える
0

編集: forループでオンラインを忘れた場合は、このコードを見てください

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

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

        CGContextAddLineToPoint(context, point.x, point.y);
    }
于 2014-07-29T06:46:55.267 に答える