2013-02-15 39 views
5

我嘗試使用不同的顏色繪製UIBezierPath行會讓我失望。所有線都變爲當前選擇的顏色。我所有的路徑和信息都存儲在一個名爲pathInfo的NSMutableArray中。在路徑信息中,我放入包含路徑,顏色,寬度和類型行的數組中。這工作正常,除了所有的行都變成用戶選擇的顏色。我會很感激任何幫助!UIBezierPath多行顏色

- (void)drawRect:(CGRect)rect { 
    UIBezierPath *drawPath = [UIBezierPath bezierPath]; 
    drawPath.lineCapStyle = kCGLineCapRound; 
    drawPath.miterLimit = 0; 

    for (int i = 0; i < [pathInfo count]; i++){ 
     NSArray *row = [[NSArray alloc] initWithArray:[pathInfo objectAtIndex:i]]; 
     NSLog(@"Path: %@",[row objectAtIndex:0]); 
     NSLog(@"Color: %@",[row objectAtIndex:1]); 
     NSLog(@"Width: %@",[row objectAtIndex:2]); 
     NSLog(@"Type: %@",[row objectAtIndex:3]); 

     //width 
     drawPath.lineWidth = [[row objectAtIndex:2] floatValue]; 

     //color 
     [[row objectAtIndex:1] setStroke]; 

     //path 
     [drawPath appendPath:[row objectAtIndex:0]]; 

    } 

    UIBezierPath *path = [self pathForCurrentLine]; 
    if (path) 
    [drawPath appendPath:path]; 

    [drawPath stroke]; 
} 

- (UIBezierPath*)pathForCurrentLine { 
    if (CGPointEqualToPoint(startPoint, CGPointZero) && CGPointEqualToPoint(endPoint, CGPointZero)){ 
     return nil; 
    } 

    UIBezierPath *path = [UIBezierPath bezierPath]; 
    [path moveToPoint:startPoint]; 
    [path addLineToPoint:endPoint]; 

    return path; 

} 
+0

如果可能的話,請通過我你的完整代碼這一點。其實我也面臨同樣的問題。但我正在使用touchesBegan,touchesMoved和touchesEnd方法。我無法做到。 – Abha 2016-06-27 11:09:12

回答

3

筆觸/填充顏色隻影響-stroke命令。它們不影響-appendPath:命令。路徑不包含每段顏色信息。

如果您需要多色線條,則需要分別對每種顏色進行筆觸。

+0

我該怎麼做@ Kevin-Ballard – JasonBourne 2013-02-15 21:43:39

+0

@JasonBourne:不要將所有路徑附加到一個主路徑中,只需依次對每個路徑進行描邊。 – 2013-02-15 21:48:24

+0

是不是我在做什麼?每個人都得到for語句@ kevin-ballard – JasonBourne 2013-02-15 21:53:49

2

設置你的筆觸顏色(和你有什麼),然後stroke,然後移動到下一個路徑:

- (void)drawRect:(CGRect)rect 
{ 
    for (int i = 0; i < [pathInfo count]; i++){ 
     NSArray *row = [[NSArray alloc] initWithArray:[pathInfo objectAtIndex:i]]; 

     NSLog(@"Path: %@",[row objectAtIndex:0]); 
     NSLog(@"Color: %@",[row objectAtIndex:1]); 
     NSLog(@"Width: %@",[row objectAtIndex:2]); 
     NSLog(@"Type: %@",[row objectAtIndex:3]); 

     UIBezierPath *path = [row objectAtIndex:0]; 

     path.lineCapStyle = kCGLineCapRound; 
     path.miterLimit = 0; 

     //width 
     path.lineWidth = [[row objectAtIndex:2] floatValue]; 

     //color 
     [[row objectAtIndex:1] setStroke]; 

     //path 
     [path stroke]; 
    } 

    UIBezierPath *path = [self pathForCurrentLine]; 
    if (path) 
    { 
     // set the width, color, etc, too, if you want 
     [path stroke]; 
    } 
} 
+1

哇你的答案也是有效的!我也很感謝你的幫助Rob!這種方法似乎效果更好。 – JasonBourne 2013-02-15 22:43:27