2015-10-03 26 views
-1

我在iOS中使用CoreGraphic绘制网格(如下所示)。但对于一些线条,我想改变线条粗细。如何更改CoreGraphic中的线条粗细?如何在iOS CoreGraphic中绘制不同厚度的线条?

//Get the CGContext from this view 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    //Set the stroke (pen) color 
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); 
    //Set the width of the pen mark 
    CGContextSetLineWidth(context, 1.0); 

    //Draw vertical lines 
    float max = totalVerticalLines*numskipPixels_h+orgx; 

    for(float i = orgx; i <= max; i+=numskipPixels_h){ 

     CGContextMoveToPoint(context, i, orgy); 
     CGContextAddLineToPoint(context, i, orgy - verLineLength); 

    } 


    //Draw vertical lines 
    float min = orgy - totalHorLines*numskipPixels_v; 
    for(float i = orgy; i > min; i-=numskipPixels_v){ 
     CGContextMoveToPoint(context, orgx, i); 
     CGContextAddLineToPoint(context, orgx+horLineLength, i); 
    } 

//Draw it 
    CGContextStrokePath(context); 
+0

使用'CGContextSetLineWidth(背景下,1.0);'就像你在你的代码已经在做,但与对'1.0'不同数量。你有什么确切的问题?预期的产出是多少?你究竟得到了什么? –

+0

我喜欢用CGContextSetLineWidth(context,1.0)绘制一些线条;但是我喜欢用CGContextSetLineWidth(context,2.0)绘制一些线条;现在的问题是我只能选择其中一种。你是否投了票?你知道如何解决? – batuman

回答

2

为不同厚度创建不同的路径,更改对CGContextStrokePath的调用之间的厚度。

像这样:

float min = orgy - totalHorLines*numskipPixels_v; 
for(float i = orgy; i > min; i-=numskipPixels_v){ 
    CGContextMoveToPoint(context, orgx, i); 
    CGContextAddLineToPoint(context, orgx+horLineLength, i); 
} 

//Draw it 
CGContextStrokePath(context); 

//Set the width of the pen mark to a different value 
CGContextSetLineWidth(context, 5.0); 

//start a new path 
CGContextBeginPath(context); 

CGContextMoveToPoint(context, point1x, point1y); 
CGContextAddLineToPoint(context, point2x, point2y); 

//Draw the new path (uses the new line thickness) 
CGContextStrokePath(context);