2012-03-21 83 views
1

我有這個問題: 我有一個點的數組,我會用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); 
} 
+0

你能告訴我們你迄今爲止編寫的代碼? – 2012-03-21 09:44:43

+0

我已添加我的測試代碼... – Safari 2012-03-21 10:12:52

回答

3

您可以使用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