简体   繁体   English

使用UIBezierPath [ios]的部分Bezier曲线

[英]Partial Bezier curve with UIBezierPath [ios]

Usually bezier paths have the parameter with 0 <= t <= 1 which describes a certain point on the curve. 通常贝塞尔路径具有0 <= t <= 1的参数,其描述曲线上的某个点。

What I would like to do is: 我想做的是:

I have the 4 points (start, end, 2 control points ) and now I would like to draw a line from t=0 to t=0.5. 我有4个点(开始,结束,2个控制点),现在我想画一条从t = 0到t = 0.5的线。 Is there a standard way to do this in iOS (native framework or open source)? 在iOS(本机框架或开源)中是否有标准的方法可以做到这一点?

If this would not be possible I would have to calculate the endpoint and also two new control points on my own. 如果这是不可能的,我将不得不自己计算端点和两个新的控制点。

If you are only interested in drawing that path and not calculating the points to do something else with them, then you could stroke the path only up until t=0.5. 如果您只对绘制该路径感兴趣而不计算要用它们做其他事情的点数,那么您可以仅在t = 0.5之前描绘路径。

You can do this with a CAShapeLayer by setting the strokeStart and strokeEnd properties. 您可以通过设置strokeStartstrokeEnd属性来使用CAShapeLayer执行此操作。 The appearance of the stroke can be properties like strokeColor and lineWidth . 笔划的外观可以是strokeColorlineWidth等属性。 I recommend that you look at the documentation for the full list of properties. 我建议您查看文档以获取完整的属性列表。

The code would look something like this (I didn't run this so there may be typos etc.): 代码看起来像这样(我没有运行这个,所以可能有拼写错误等):

CAShapeLayer *halfBezier = [CAShapeLayer layer];
// use the full path
halfBezier.path          = [yourFullPath CGPath];
// configure the appearance 
halfBezier.fillColor     = [[UIColor clearColor] CGColor];
halfBezier.strokeColor   = [[UIColor redColor] CGColor];
halfBezier.lineWidth     = 2.0;

// 0.0 ≤ t ≤ 0.5
halfBezier.strokeStart   = 0.0; // the default value (only here for clarity)
halfBezier.strokeEnd     = 0.5; // only up until t=0.5

// add this layer to the view's layer where it is supposed to be drawn
[yourView.layer addSublayer:halfBezier];

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

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