如何在UIBezierPath中填充颜色?

LE *_*ANG 12 ios uibezierpath

我在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]; 
}
Run Code Online (Sandbox Code Playgroud)

我想在closePath之后填充其中的RED颜色但不能.请帮忙!

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

Abi*_*ern 18

如果你有一个bezier路径存储在别处,这应该工作:

编辑

看看你编辑过的代码,发生的事情就是当你关闭你正在绘制的路径时,你正在关闭 - 所以你得到一条线,而不是一个形状,因为你只有两个点.

解决此问题的一种方法是在点移动时创建路径,但是笔划并填充该路径的副本.例如,这是未经测试的代码,我直接写它

-(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]; 
}
Run Code Online (Sandbox Code Playgroud)

然后你的绘图代码可以:

- (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.

}
Run Code Online (Sandbox Code Playgroud)

有一些整理要做touchesEnded,这可以使性能更高,但你明白了.