如何进行抛物线转换?
我正在开发一个带有一些简单动画的 iPhone 应用程序。
我有一个观点想要翻译,但不是沿着一条线。我想用抛物线的方式翻译它。想象一下,我正在制作一辆沿着弯曲道路行驶的汽车的动画。
我知道我可以将变换正确设置为 CGAffineTransform 的实例
问题是,我不知道如何创建变换。我知道如何缩放、平移等,但如何进行抛物线平移?有可能吗?
I'm working on an iPhone app with some simple animation.
I have a view I want to translate, but not along a line. I want to translate it parabolically. Imagine that I am animating a car moving along a curved road.
I know I can set the transform properly to an instance of CGAffineTransform
Problem is, I have no idea how to create the transform. I know how to scale, translate, etc. but how do I translate parabolically? Is it even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要沿着平滑曲线制作动画,您需要使用 CAKeyframeAnimation。在 这个答案我提供了组合动画的代码,该动画沿着曲线移动图像视图,同时调整其大小并使其淡出。该代码的相关部分如下:
这将创建一条具有两个控制点的曲线,一个位于视图的原点,另一个位于显示屏的右上角(在横向模式下)。有关构建路径的更多信息,请参阅 Quartz 2D编程指南或动画类型和时序编程指南。
要使用此动画,您需要使用类似以下内容将其添加到视图的图层中:
To animate along a smooth curve, you'll want to use a CAKeyframeAnimation. In this answer I provide code for a combined animation that moves an image view along a curve, while resizing it and fading it out. The relevant part of that code is as follows:
This creates a curve with two control points, one at the origin of the view and the other at the upper-right corner of the the display (in landscape mode). For more on constructing paths, see the Quartz 2D Programming Guide or the Animation Types and Timing Programming Guide.
To use this animation, you'll need to add it to your view's layer using something like the following:
沿着曲线制作动画的一种美观的方式是使用样条线。这些在数学上简单且计算高效。求解抛物线拱的最小阶样条是二次样条。
An aesthetically pleasing way to animate along curves is to use splines. These are mathematically simple and computationally efficient. The least order spline that would solve a parabolic arch is a quadratic spline.
要包括图像沿路径的旋转,只需添加以下内容
您还可以添加计时函数来创建缓入/缓出效果
to include rotation of the image along the path, just add the following
you can also add a timing function to create the easein/out effect
听起来您需要定期将变换属性设置为使用沿抛物线选择的 x,y 参数创建的 CGAffineTransformTranslate(按照 Russell 建议从 y 获取 x 或反之亦然)。
如果您还希望汽车沿着抛物线弯曲时旋转(看起来更真实),您将需要 CGAffineTransormRotate。根据当前绘制点处的抛物线斜率确定角度。您需要一个 atan() 函数来计算与斜坡的角度。听起来很痛苦,希望有更简单的方法。
我真的不知道我在说什么,我刚刚读了这篇博客文章: 揭秘 CGAffineTransform。
It sounds like you need to periodically set the transform property to a
CGAffineTransformTranslate
created with x,y parameters chosen along the parabola (do what Russell suggested to get x from y or vice versa).If you also want your car to rotate as it curves along the parabola (will look more realistic) you will need
CGAffineTransormRotate
. Determine the angle based on the slope of the parabola at the point you're currently drawing at. You'll need an atan() function to compute the angle from the slope. Sounds like a pain, hopefully there's an easier way.I really don't know what I'm talking about, I just read this blog entry: Demystifying CGAffineTransform.
我认为 TokenMacGuy 使用样条线的建议是一个很好的建议。再说一次,我不知道我在说什么,但也许这会给你一些想法。
它必须是精确的抛物线还是近似值有效?你想做的事情听起来很像 渲染沿曲线的文本(请参阅屏幕截图 也)。这一系列的帖子似乎是一个非常好的指南——解释了角度计算和其他东西。每个“字符”对应于您汽车的一个位置。对弧长进行参数化可确保速度恒定。
至于如何在 iPhone 上做到这一点……不知道。
I think TokenMacGuy's suggestion to use splines is a good one. Again, I don't know what I'm talking about but maybe this will give you some ideas.
Does it have to be exactly a parabola or will an approximation work? What you want to do sounds very much like rendering text along a curve (see screenshot too). That series of posts seems like a pretty good how-to -- explains angle calculations and stuff. Each "character" corresponds to a position of your car. Parameterizing on arc length ensures your velocity is constant.
As far as how to do that on an iPhone... no idea.