简体   繁体   English

在iOS中将线弯曲成圆

[英]Curve line into circle in iOS

I have this idea of transforming a straight line into a circle but the offset of a cell drag in a table view. 我有将直线转换成圆形的想法,但是在表格视图中单元格的偏移拖动了。

As I drag the cell, I want the line to curve into a circle around an image. 拖动单元格时,我希望线条在图像周围弯曲成一个圆。 I've included a picture below to help demonstrate different states with different drag offsets. 我在下面提供了一张图片,以帮助演示具有不同拖动偏移量的不同状态。

Im not sure where to start, was thinking of maybe using UIBezierPath to draw but not sure if that's the best solution. 我不确定从哪里开始,我在考虑也许使用UIBezierPath进行绘制,但不确定这是否是最佳解决方案。

在此处输入图片说明

If you want it to animate then you have your work cut out for you. 如果您希望它具有动画效果,那么您就可以轻松完成工作。

Core Animation of curves is based on CGPath objects, which is the underlying Core Foundation class behind UIBezierPath. 曲线的核心动画基于CGPath对象,该对象是UIBezierPath之后的基础Core Foundation类。

The secret to making a curve animate from one shape to another is to use the same number and type of control points. 使曲线从一种形状变为另一种形状的秘密是使用相同数量和类型的控制点。 You won't be able to use any of the standard arc or oval shortcuts (which generate more complex bezier curves that look like arcs.) 您将无法使用任何标准的圆弧或椭圆快捷方式(它们会生成更复杂的看起来像圆弧的贝塞尔曲线。)

Instead, you'll have to build an approximation of a circle piecewise out of a linked series of cubic bezier curves. 取而代之的是,您必须从一系列相连的三次方贝塞尔曲线中逐段建立一个近似的圆。 You should be able to get fairly close with 4 linked cubic bezier curves who's endpoints line are at the N/S/E/W compass points of a circle, and the intermediate control points are spaced evenly outside the circle. 您应该能够使用4条链接的三次方贝塞尔曲线相当接近,它们的端点线位于圆的N / S / E / W圆规点,并且中间控制点在圆外均匀分布。 A couple of years ago I looked up an article on the net for approximating a circle using Bezier points. 几年前,我在网上查找了一篇文章,以使用Bezier点逼近一个圆。 I would suggest doing some searching on that. 我建议对此进行一些搜索。

Alternately, I guess you could generate a circle bezier curve using one of the CGPath or UIBezier shortcuts, then deconstruct the resulting path into the primitives that make it up. 或者,我猜您可以使用CGPath或UIBezier快捷方式之一生成圆形贝塞尔曲线,然后将生成的路径解构成组成它的图元。 Erica Sadun's outstanding iOS Developer's Cookbook series includes a recipe that shows how to deconstruct a UIBezier path into it's primitives. 埃里卡·萨顿(Erica Sadun)出色的iOS开发者食谱系列包括一个食谱,该食谱显示了如何将UIBezier路径解构成其原语。

Once you have a set of control points for a circle, you would need to re-map them into control points that make your line. 一旦为圆设置了一组控制点,就需要将它们重新映射到构成直线的控制点上。 (A Bezier curve always passes through it's beginning and end points, and if you put the inner 2 control points of a Cubic Bezier on a line, it will turn the curve into a line.) (贝塞尔曲线始终穿过其起点和终点,如果将三次贝塞尔曲线的内部2个控制点放在一条线上,它将把曲线变成一条直线。)

Now you have 2 shapes made up of the same number of bezier curves and the same number of control points: A circle and a line. 现在,您有2个形状,它们由相同数量的贝塞尔曲线和相同数量的控制点组成:一个圆和一条直线。 You can transform the line into the circle or the circle into the line by moving each of the control points to different x/y coordinates. 通过将每个控制点移动到不同的x / y坐标,可以将线转换为圆或将圆转换为线。

Then you might be able to apply a linear interpolation between the starting and ending coordinates of your control points. 这样,您便可以在控制点的开始和结束坐标之间应用线性插值。 Use the user's drag of the table view to generate a value from 0 to 1, and apply that to your interpolated control point values (at 0.0, your control points would be at their "straight line" position and your curve would draw as a straight line. At 1.0, they'd be at their circle position, and your curve would draw as a circle. At points between, they'd be a fraction of the way between their beginning and ending positions, and you'd get a shape that was between a line and a circle. 使用用户在表格视图中的拖动来生成从0到1的值,并将其应用于插值的控制点值(在0.0时,控制点将位于其“直线”位置,并且曲线将绘制为直线在1.0处,它们将位于其圆的位置,并且您的曲线将绘制为一个圆。在它们之间的点处,它们将是其起点和终点之间的距离的一小部分,并且您将获得一个形状在直线和圆之间。

Once you have figured out how to generate the control points to create a curve that moves smoothly from a straight line to a circle, you are ready to tackle doing it using Core Animation and a CAShapeLayer. 一旦弄清楚了如何生成控制点以创建从直线平滑过渡到圆弧的曲线,就可以使用Core Animation和CAShapeLayer解决此问题。

If that makes sense then you can probably figure out how to do this. 如果这有意义,那么您可能可以找出解决方法。 If you have no idea what I am talking about they you are probably in over your head. 如果您不知道我在说什么,那么您可能会不知所措。

(I'm a senior Cocoa/iOS developer. I've done a lot of Core Animation and it would probably take me 3 or 4 hours to get what you are after to work, once I had the circle bezier control points to start from.) (我是Cocoa / iOS的高级开发人员。我做了很多Core Animation,一旦我有了圆形的贝塞尔曲线控制点,可能要花3到4个小时才能得到您要工作的东西。 。)

Come to think of it, it would probably be a lot simpler to use UIView keyframe animation. 想一想,使用UIView关键帧动画可能会简单得多。 That lets you specify an array of control points that ALL lie on the desired curve, and generates a smooth curve from those points.. Best yet, it is a UIView animation, which is a heck of a lot easier to use than CAAnimation. 这样就可以指定所有位于所需曲线上的控制点的数组,并从这些点生成平滑曲线。.最好的是,它是UIView动画,比CAAnimation易于使用。

Take a look at my demo project RandomBlobs on github. 看看我在github上的演示项目RandomBlobs That should give you a head start on using UIView keyframe animation. 这应该使您开始使用UIView关键帧动画。 The method you want is called animateKeyframesWithDuration:delay:options:animations:completion: . 所需的方法称为animateKeyframesWithDuration:delay:options:animations:completion:

The down-side of point-based keyframe animation is that sometimes the curve you get has "kinks" or loops in it that you don't expect or want. 基于点的关键帧动画的缺点是,有时您获得的曲线会出现您不希望或不希望出现的“扭结”或循环。 You have to avoid sharp bends. 您必须避免急剧弯曲。 In sketching it out, though, I think a line-to-circle transition might work with keyframe view animation. 不过,在勾勒出草图时,我认为线到圆过渡可能适用于关键帧视图动画。

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

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