在iphone上绘制一个不规则的多边形
问题描述:
我有这个问题: 我有一个点的数组,我会用Quartz或类似的点绘制一个不规则的多边形。 你能建议我做到这一点的最佳方法吗?在iphone上绘制一个不规则的多边形
MyTest的drawRect中是这样的:
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
CGContextSetRGBStrokeColor(ctx, 255, 0, 255, 1);
CGPoint points[6] = { CGPointMake(100, 200), CGPointMake(150, 250),
CGPointMake(150, 250), CGPointMake(50, 250),
CGPointMake(50, 250), CGPointMake(100, 200) };
CGContextStrokeLineSegments(ctx, points, 6);
}
答
您可以使用UIBezierPath/NSBezierPath:
UIBezierPath *poly = [[UIBezierPath alloc] init];
[poly moveToPoint:CGPointMake(0.0, 0.0)];
[poly addLineToPoint:CGPointMake(1.0, 0.0)];
[poly addLineToPoint:CGPointMake(1.0, 1.0)];
[poly closePath];
[poly stroke]; // draw stroke
[poly release];
+0
如何更改线条颜色? 你可以通过我的drawRect例子找到与我不同的方式吗? – Safari 2012-03-21 10:10:20
+1
[[UIcolor redColor] setStroke]; – jacekmigacz 2012-03-21 10:18:28
你能告诉我们你迄今为止编写的代码? – 2012-03-21 09:44:43
我已添加我的测试代码... – Safari 2012-03-21 10:12:52