0

我正在尝试在UIView. 我通过覆盖drawRect我的子类中的方法来做到这一点UIView。但是当显示视图时,我只看到 的backgroundColorview但我没有看到我的形状被绘制在其中。

这是我-drawRect方法中的代码

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

正在绘制的数组如下:

  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

编辑:你在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 回答
0

您不会更改循环中的点!只需添加:

点 = [点[i] CGPointValue]

在你的循环里面?

于 2014-07-29T08:54:36.557 回答