为什么Bezier曲线在iOS UIView中成为一种形状?

时间:2017-06-15 19:19:58

标签: ios drawing

我试图在iOS UIView中绘制Bezier曲线。我的代码是这样的:

UIBezierPath *curvePath = [UIBezierPath bezierPath];
[curvePath moveToPoint:_startPoint];
[curvePath addCurveToPoint:_endPoint controlPoint1:_controlPoint1 controlPoint2:_controlPoint2];
[[UIColor blueColor] setStroke];
curvePath.lineWidth = 8;
[curvePath stroke];
_drawingLayer = [CAShapeLayer layer];
_drawingLayer.path = curvePath.CGPath;
[self.view.layer addSublayer:_drawingLayer];

我期待蓝色的 弯曲贝塞尔线 ,但结果却是 封闭的黑色形状 像这样: enter image description here 我的代码有什么问题吗?感谢。

1 个答案:

答案 0 :(得分:0)

您将手绘与CAShapeLayer混合在一起。如果您正在使用形状图层,则需要对其进行配置,而不是尝试自己绘制路径。例如:

UIBezierPath *curvePath = [UIBezierPath bezierPath];
[curvePath moveToPoint:_startPoint];
[curvePath addCurveToPoint:_endPoint controlPoint1:_controlPoint1 controlPoint2:_controlPoint2];
_drawingLayer = [CAShapeLayer layer];
_drawingLayer.path = curvePath.CGPath;

/* Configure the layer, not the current context */
_drawingLayer.strokeColor = UIColor.blueColor.CGColor;
_drawingLayer.lineWidth = 8;

[self.view.layer addSublayer:_drawingLayer];