[英]EaseOut action with custom SKAction
I have the following custom SKAction working but as an EaseIn instead EaseOut. 我有以下自定义SKAction工作,但作为EaseIn而不是EaseOut。 I want it to EaseOut!
我想要它EaseOut! I have failed miserably to correct it using various easing equations found around the web.
我使用在网络上找到的各种缓动方程式来纠正它是惨不忍睹的。
let duration = 2.0
let initialX = cameraNode.position.x
let customEaseOut = SKAction.customActionWithDuration(duration, actionBlock: {node, elapsedTime in
let t = Double(elapsedTime)/duration
let b = Double(initialX)
let c = Double(targetPoint.x)
let p = t*t*t*t*t
let l = b*(1-p) + c*p
node.position.x = CGFloat(l)
})
cameraNode.runAction(customEaseOut)
Any help would be much appreciate. 任何帮助都会非常感激。
Thanks 谢谢
You don't need to calculate it. 你不需要计算它。
SKAction
just have a property called timingMode
: SKAction
只有一个名为timingMode
的属性:
// fall is an SKAction
fall.timingMode = .easeInEaseOut
You can choose from: 您可以选择:
Check details from API docs and also here . 查看API文档中的详细信息以及此处 。
If you need to change the Apple presets you can use: timingFunction 如果您需要更改Apple预设,可以使用: timingFunction
fall.timingFunction = { time -> Float in
return time
}
To build a custom function according to the source: 要根据源构建自定义函数:
/**
A custom timing function for SKActions. Input time will be linear 0.0-1.0
over the duration of the action. Return values must be 0.0-1.0 and increasing
and the function must return 1.0 when the input time reaches 1.0.
*/
public typealias SKActionTimingFunction = (Float) -> Float
So with these informations you can write: 所以使用这些信息你可以写:
func CubicEaseOut(_ t:Float)->Float
{
let f:Float = (t - 1);
return f * f * f + 1;
}
fall.timingFunction = CubicEaseOut
We can modify the following code to allow the custom action to ease-out instead of ease-in 我们可以修改以下代码以允许自定义操作缓出而不是轻松实现
let t = Double(elapsedTime)/duration
...
let p = t*t*t*t*t
To get a better understanding of p
, it is helpful to plot it as a function of t
为了更好地理解
p
,将其绘制为t
的函数是有帮助的
Clearly, the function eases in over time. 显然,该功能随着时间的推移而缓和。 Changing the definition of
t
to 将
t
的定义更改为
let t = 1 - Double(elapsedTime)/duration
and plotting p
gives 并绘制
p
给出
The action now eases out, but it starts at 1 and ends at 0. To resolve this, change the definition of p
to 该操作现在缓和,但从1开始到0结束。要解决此问题,请将
p
的定义更改为
let p = 1-t*t*t*t*t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.