iOS中的CoreGraphic如何绘制不同粗细的线条?

How to draw lines with different thicknesses in CoreGraphic in iOS?

我在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);

为不同的厚度创建不同的路径,在调用 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);