简体   繁体   English

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

[英]Why Bezier curve turns out to be a shape in iOS UIView?

I am trying to draw a Bezier curve in an iOS UIView. 我试图在iOS UIView中绘制Bezier曲线。 My code is like this: 我的代码是这样的:

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

I was expecting a curved Bezier line with blue color but it turns out to be a closed black shape like this: 我期待一条弯曲的贝塞尔线蓝色,但结果是一个封闭的黑色形状,如下所示: 在此输入图像描述 What's wrong with my code please? 我的代码有什么问题吗? Thanks. 谢谢。

You've intermixed hand-drawing with CAShapeLayer . 你已经用CAShapeLayer混合了手绘图。 If you're using a shape layer, you need to configure it, not try to draw the path yourself. 如果您正在使用形状图层,则需要对其进行配置,而不是尝试自己绘制路径。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM