2015-10-13 173 views
0

我想用手指在UIView上畫線,並且它可以很好地處理一種顏色。如果我嘗試更改顏色並再次繪製,則之前的UIBezierPath顏色也會更改爲新顏色。所以,我無法得出一個不同顏色的線保持以前的綵線在UIView如何使用一個UIBezierPath創建兩個不同顏色的筆劃

我設置的所有屬性(路徑,linecolor)作爲非原子和強在我UIView

參考:

我的第一個平局:enter image description here

我的第二輪抽籤:enter image description here

我的第三個平局:enter image description here

我選擇的顏色後,我改變我的UIView的筆觸顏色的顏色選擇器的委託方法:

#pragma mark - FCColorPickerViewControllerDelegate Methods 

-(void)colorPickerViewController:(FCColorPickerViewController *)colorPicker didSelectColor:(UIColor *)color { 

     self.drawView.lineColor = color; //this works fine 

// self.drawView.path=[UIBezierPath bezierPath]; tried this to create new bezier path with new color, but this erases the olde bezier path and return new 
// [self.drawView.lineColor setStroke];  tried this 
// [self.drawView.lineColor setFill]; tried this 

     [self dismissViewControllerAnimated:YES completion:nil]; //dismiss the color picker 
} 

這裏是我的觀點方法繪製:

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    if (self = [super initWithCoder:aDecoder]) 
    { 
     [self setBackgroundColor:[UIColor whiteColor]]; 
     path = [UIBezierPath bezierPath]; 
     [path setLineWidth:self.lineWidth]; 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)frame 
{ 
    [self.lineColor setStroke]; 

    [path stroke]; 

} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     UITouch *touch = [touches anyObject]; 
     CGPoint p = [touch locationInView:self]; 
     [path moveToPoint:p]; 
    }); 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     UITouch *touch = [touches anyObject]; 
     CGPoint p = [touch locationInView:self]; 
     [path addLineToPoint:p]; 
     [self setNeedsDisplay]; 
    }); 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self touchesMoved:touches withEvent:event]; 
} 

我試過this:增加舊的貝塞爾路徑陣列和重畫它們,但它沒有工作,這次我不能再用新的顏色另一條貝塞爾路徑。:

 - (void)drawRect:(CGRect)frame // (5) 
{ 
    //load the path from array 
    for (int i = 0; i < [pathArray count]; i++){ 


     NSLog(@"Path: %@",[pathArray objectAtIndex:0]); 
     NSLog(@"Color: %@",[pathArray objectAtIndex:1]); 

     UIBezierPath *oldpath = [pathArray objectAtIndex:0]; 

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

     //path 
     [oldpath stroke]; 
    } 

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

} 

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

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

    return newpath; 

} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     UITouch *touch = [touches anyObject]; 
     CGPoint p = [touch locationInView:self]; 
     [path moveToPoint:p]; 
     startPoint=p; 
    }); 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     UITouch *touch = [touches anyObject]; 
     CGPoint p = [touch locationInView:self]; 
     [path addLineToPoint:p]; // (4) 
     endPoint=p; 

     [self setNeedsDisplay]; 
    }); 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self touchesMoved:touches withEvent:event]; 

    [pathArray addObject:path]; 
    [pathArray addObject:lineColor]; 

} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self touchesEnded:touches withEvent:event]; 
} 
+0

每當用戶提起手指或每次改變顏色時,都應該創造一條新的貝塞爾路徑。 – Aky

回答

4

實際上,您不能以不同顏色描畫同一路徑的不同部分。

您可以將繪圖上下文視爲狀態機。每次您發出繪畫命令(例如在UIBezierPath上調用stroke)時,它都會檢查當前筆觸/填充顏色並使用它執行繪製。要使用不同的顏色創建多個筆劃,您需要有多個筆劃路徑,並在每次調用stroke之間設置筆劃顏色。

- (void)drawRect:(CGRect)frame { 
    UIBezierPath* pathOne = // create path one 
    UIBezierPath* pathTwo = // create path two 
    UIBezierPath* pathThree = // create path three 

    [[UIColor redColor] setStroke]; 
    [pathOne stroke]; 

    [[UIColor greenColor] setStroke]; 
    [pathTwo stroke]; 

    [[UIColor blueColor] setStroke]; 
    [pathThree stroke]; 
} 
+0

如果我創建一個新的貝塞爾路徑,我的舊路線會發生什麼?我將bezierpath作爲屬性聲明。我是否需要將該屬性設置爲新路徑?請舉例 –

+0

加班'drawRect:'被調用,您必須重新繪製需要在屏幕上顯示的所有內容。如果您需要保留舊路徑,則必須保存每次調用'drawRect:'時都會繪製的路徑數組。 –

+0

讓我試試。 –

相關問題