【问题标题】:CGContext is not drawing my shapeCGContext 没有画出我的形状
【发布时间】:2014-07-29 06:35:31
【问题描述】:

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

这是我的-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)]];

【问题讨论】:

  • 确定setNeedsDisplay
  • 当我运行应用程序并在drawRect方法中添加断点时,我可以看到代码被调用,所以我建议调用setNeedsDisplay。

标签: ios objective-c xcode cgcontext hexagonal-tiles


【解决方案1】:

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

point = [points[i] CGPointValue]

在你的循环中?

【讨论】:

    【解决方案2】:

    编辑:你在for循环中忘记在线查看这段代码

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

    【讨论】:

    • 你能提供你的点数组吗,我可以实现它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    相关资源
    最近更新 更多