【发布时间】:2014-07-29 06:35:31
【问题描述】:
我正在尝试在UIView 中绘制一个六边形。我通过覆盖UIView 的子类中的drawRect 方法来做到这一点。
但是当显示视图时,我只看到view 的backgroundColor,但我没有看到我的形状正在其中绘制。
这是我的-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