1

我能够在我的应用程序中使用 UIBezierPath 进行绘制。现在我添加了一个 UIColor 变量,以便用户按下按钮可以选择不同的颜色。当用户更改颜色并开始使用新颜色绘制时,所有已绘制的路径都具有新颜色。

如何告诉对象不要更改已绘制路径的颜色?

这是我的代码。非常感谢任何代码示例!:)

(我的 UIColor 变量存储在 appDelegate 中,我正在从另一个 viewControlle 调用刷新方法。

 - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];


    self.backgroundColor = [UIColor whiteColor];
    myPath = [[UIBezierPath alloc] init];
    myPath.lineCapStyle = kCGLineCapRound;
    myPath.miterLimit = 0;
    myPath.lineWidth = 5;

    brushPattern = app.currentColor;

}
   return self;
}
-(void)refresh {
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    brushPattern = app.currentColor;
}



- (void)drawRect:(CGRect)rect {
     [brushPattern setStroke];
     for (UIBezierPath *_path in pathArray)
     [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   myPath = [[UIBezierPath alloc] init];
   myPath.lineWidth = 5;

    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    [myPath moveToPoint:[mytouch locationInView:self]];
    [pathArray addObject:myPath];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
     UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
     [myPath addLineToPoint:[mytouch locationInView:self]];
     [self setNeedsDisplay];
 }

 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

 }
4

1 回答 1

-1

我找到了一个演示代码..

如果它对你有帮助,请检查这个..

平滑线链接

于 2013-04-09T05:12:41.540 回答