如何在UIView中绘制多个带有不同颜色的UIBezierPath?

10
我想在一个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?
3个回答

24

你应该添加

[desiredStrokeColor setStroke];
[desiredFillColor setFill];

这表明这些是在此上下文中必须使用的新颜色。在每次调用之前,您都应该这样做。

[aNewPath fill];
[aNewPath stroke];

让路径用那些颜色绘制。

不需要为每条贝塞尔路径使用一个新视图。


好的,谢谢。现在如果我想在绘制形状后更改填充和描边颜色,我该怎么做?例如,假设我点击了某个形状,我该如何更改它的颜色? - iMacX
你需要维护一个可变的颜色数组,每个路径都有一个。你应该更改特定路径的颜色,然后在视图上调用setNeedsDisplay,因为它会调用drawRect:方法。 - Deepak Danduprolu
好的,再次感谢你。或许你能够帮我回答关于同一主题的另一个问题,在这里:http://stackoverflow.com/questions/6432770/how-to-draw-and-interact-with-uibezierpath - iMacX
我怎样可以在drawrect方法之外改变形状的颜色呢?如果我调用setNeedsDisplay,我的视图会被重新初始化吗?谢谢。 - iMacX
@iMacX:既然您似乎仍在此网站上活跃,那么回答您的问题是,您需要创建一个新类来存储贝塞尔路径、填充颜色和描边颜色。将这些存储为数组,您可以随时更改颜色,并在绘制时检索。您所说的“重新初始化”是什么意思?setNeedsDisplay会导致视图被清除并重新绘制(使用drawRect,没有更多或更少)。 - Cowirrie

8

使用这个

int count = 0;
for(UIBezierpath *_paths in pathArray)
{
   UIColor *_color = [delegate1.colorArray objectAtIndex:q];
   [_color setStroke];
   [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 
   count++;
}

在touch began事件中,将你的路径和颜色存储在两个数组中,并像上面那样在drawrect中使用它们。

5
在.h文件中定义一个UIColor *setStroke;对象,并在调用[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];之前设置这个strokeColor对象。
 - (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];

    }

pathArray 是什么? - Abha
UIBezierPath对象的集合.. @Abha - YogiAR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接