CGFloat start = 0; // The start value of X for an animation
CGFloat distance = 100; // The distance X will have traveled when the animation completes
CAMediaTimingFunction* tf = [CAMediaTimingFunction functionWithControlPoints:0 :0 :1 :1]; // Linear easing for simplicity
CGFloat percent = [tf valueAtTime:0.4]; // Returns 0.4
CGFloat x = start + (percent * distance); // Returns 40, which is the value of X 40% through the animation
如何将方法 valueAtTime:
实现到 CAMediaTimingFunction
的类别中,以便它像上面的代码中描述的那样工作?
请注意:这是一个人为的例子。实际上,我将使用带有 UIPanGestureRecognizer 的非线性计时函数来实现非线性拖动效果。谢谢。
最佳答案
计时函数是一条非常简单的贝塞尔曲线 - 两个端点 0,0
和 1,1
,每个端点有一个控制点 - 绘制时间 (x)动画完成的百分比 (y),因此您所要做的就是贝塞尔曲线数学运算(给定 x,对应的 y 是多少)。谷歌一下,你会很容易找到必要的公式。这是我发现的一个不错的起点:http://pomax.github.io/bezierinfo/
关于ios - 在 CAMediaTimingFunction 的特定时间获取百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22418579/