ios 如何在 UIBezierPath 中填充颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15358769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:40:22  来源:igfitidea点击:

How to fill color inside UIBezierPath?

iosuibezierpath

提问by LE SANG

I draw a shape by UIBezierPath in touchesMoved.

我在 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]; 
}

I want to fill RED color inside it after closePath but can't. Please help!

我想在 closePath 之后在其中填充红色,但不能。请帮忙!

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

回答by Abizern

If you have a bezier path stored elsewhere, this should work:

如果您在其他地方存储了贝塞尔曲线路径,这应该有效:

Edit

编辑

Looking at your edited code, what is happening is that as you are closing the path that you are drawing is getting closed - so you get a line, not a shape since you have only two points.

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

One way around this is to create the path as your points move, but stroke and fill a copy of that path. For example this is untested code, I'm writing it straight in

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

-(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]; 
}

And then your drawing code can:

然后你的绘图代码可以:

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

}

There is some tidying up to do an touchesEndedand this could be made more performant, but you get the idea.

有一些整理工作要做touchesEnded,这可以提高性能,但你明白了。

回答by Hasintha Janka

You have to keep array of your UIBezierPaths. Try this code,

你必须保留你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];

    }