如何在iOS中的CoreGraphic中绘制不同厚度的线条?

时间:2015-10-03 12:58:22

标签: ios core-graphics

我在iOS中使用CoreGraphic绘制网格(如下所示)。但是对于某些线路,我想改变线条厚度。如何在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);

1 个答案:

答案 0 :(得分: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);