gpt4 book ai didi

ios - 如何在 UIBezierPath 中填充颜色?

转载 作者:技术小花猫 更新时间:2023-10-29 10:35:20 32 4
gpt4 key购买 nike

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

最佳答案

如果您在别处存储了贝塞尔曲线路径,这应该可行:

编辑

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

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

-(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 需要一些整理工作,这可以提高性能,但您明白了。

关于ios - 如何在 UIBezierPath 中填充颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15358769/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com