2015-10-13 15 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

让我试试。 –

相关问题