如何在UIBezierPath内部填充颜色?

15

我在touchesMoved中使用UIBezierPath绘制了一个形状。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    secondPoint = firstPoint;
    firstPoint = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];

    CGPoint mid1 = midPoint(firstPoint, secondPoint);
    CGPoint mid2 = midPoint(currentPoint, firstPoint);

    [bezierPath moveToPoint:mid1]; 
    [bezierPath addQuadCurveToPoint:mid2 controlPoint:firstPoint];

    [self setNeedsDisplay]; 
}

在closePath之后,我想将红色填充颜色进去,但无法实现。请帮忙!

- (void)drawRect:(CGRect)rect
{
    UIColor *fillColor = [UIColor redColor];
    [fillColor setFill];
    UIColor *strokeColor = [UIColor blueColor];
    [strokeColor setStroke];
    [bezierPath closePath];
    [bezierPath fill];
    [bezierPath stroke]; 
}

看起来你并没有实际描绘路径,因此也就没有需要闭合的路径。 - Abizern
1
你能给个答案吗?我不是很理解。谢谢! - LE SANG
让我猜猜?你得到的不是一个形状,只有一条线?每次触摸移动时,它都会关闭当前子路径。 - Abizern
是的,先生!只有蓝线。我明白closePath可以制作形状?非常糟糕。 - LE SANG
你的代码没问题,除了 setFill 方法并没有像你期望的那样设置填充上下文(参见:https://stackoverflow.com/questions/10623545/uicolor-setfill-doesnt-work/10623961)。相反,使用 obj-C 的等效方法来设置填充,如下所示:let context = UIGraphicsGetCurrentContext()!; context.setFillColor(UIColor.blue.cgColor) - pseudosudo
2个回答

19

如果您在其他地方存储了贝塞尔路径,那么这应该适用:

编辑

查看您编辑后的代码,正在发生的情况是,当您关闭路径时,正在绘制的路径也被关闭了。因此,您只得到一条线而不是形状,因为您只有两个点。

解决此问题的方法之一是在点移动时创建路径,但对该路径进行描边和填充的副本。例如以下是未经测试的代码,我直接编写

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    secondPoint = firstPoint;
    firstPoint = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];

    CGPoint mid1 = midPoint(firstPoint, secondPoint);
    CGPoint mid2 = midPoint(currentPoint, firstPoint);

    [bezierPath moveToPoint:mid1]; 
    [bezierPath addQuadCurveToPoint:mid2 controlPoint:firstPoint];

    // pathToDraw is an UIBezierPath * declared in your class
    pathToDraw = [[UIBezierPath bezierPathWithCGPath:bezierPath.CGPath];

    [self setNeedsDisplay]; 
}

然后你的绘图代码可以:

- (void)drawRect:(CGRect)rect {
    UIColor *fillColor = [UIColor redColor];
    [fillColor setFill];
    UIColor *strokeColor = [UIColor blueColor];
    [strokeColor setStroke];

    // This closes the copy of your drawing path.
    [pathToDraw closePath];

    // Stroke the path after filling it so that you can see the outline
    [pathToDraw fill]; // this will fill a closed path
    [pathToDraw stroke]; // this will stroke the outline of the path.

}

touchesEnded方法中有一些整理工作需要完成,这可以更高效地完成,但你已经有了这个思路。


如何在CAShapeLayer中为封闭的贝塞尔路径填充颜色。对我来说,它总是黑色的。 - Madhu

-4

你需要保留你的UIBezierPath数组。尝试这段代码:

 - (void)drawRect:(CGRect)rect
    {

        for(UIBezierPath *_path in pathArray){


         [_path fill];
        [_path stroke];
        }
    }

    #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];

    }

它没有回答关于填充路径的问题。这只是用触摸绘制一条线的示例代码。 - Abizern
在drawrect中的代码是用来填充绘图的代码。 - Hasintha Janka

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