问题描述
||
我想在具有不同笔触和填充颜色的UIView中绘制多个UIBezierPath。
这是代码
- (void)drawRect:(CGRect)rect {
context = UIGraphicsGetCurrentContext();
[[UIColor grayColor] setFill];
[[UIColor greenColor] setstroke];
UIBezierPath *aPath = [[UIBezierPath alloc] init];
[aPath movetoPoint:CGPointMake(227,34.25)];
[aPath addLinetoPoint:CGPointMake(298.25,34.75)];
[aPath addLinetoPoint:CGPointMake(298.5,82.5)];
[aPath addLinetoPoint:CGPointMake(251,83)];
[aPath addLinetoPoint:CGPointMake(251,67.5)];
[aPath addLinetoPoint:CGPointMake(227.25,66.75)];
[aPath closePath];
aPath.linewidth = 2;
[aPath fill];
[aPath stroke];
UIBezierPath *aPath2 = [[UIBezierPath alloc] init];
[aPath2 movetoPoint:CGPointMake(251.25,90.5)];
[aPath2 addLinetoPoint:CGPointMake(250.75,83.25)];
[aPath2 addLinetoPoint:CGPointMake(298.5,83)];
[aPath2 addLinetoPoint:CGPointMake(298.5,90.25)];
[aPath2 closePath];
aPath2.linewidth = 2;
[aPath2 fill];
[aPath2 stroke];
[paths addobject:aPath2];
问题是笔划和填充颜色是在当前上下文中设置的。
是否可以在同一CGContextRef中以不同的颜色绘制不同的UIBezierPath?
还是我必须在单独的UIView中绘制每个UIBezierPath?
解决方法
您应该添加
[desiredStrokeColor setStroke];
[desiredFillColor setFill];
表示这些是在这种情况下必须进一步使用的新颜色。您应该在每次致电
[aNewPath fill];
[aNewPath stroke];
以便使用这些颜色绘制路径。
无需为每个贝塞尔曲线路径使用新视图。
, 用这个
int count = 0;
for(UIBezierpath *_paths in pathArray)
{
UIColor *_color = [delegate1.colorArray objectAtIndex:q];
[_color setStroke];
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
count++;
}
在联系中,您开始将路径和颜色存储在两个数组中,并像上面那样在drawrect中使用它们。
, 只需定义一个UIColor * setStroke;在.h文件中并设置此strokeColor对象,然后再调用[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
- (void)drawRect:(CGRect)rect
{
[strokeColor setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
for(UIBezierPath *_path in pathArray)
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
myPath=[[UIBezierPath alloc]init];
myPath.lineWidth = currentSliderValue;
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];
}