2015-10-03 57 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);