Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import SpriteKit
struct ActionTimingFunction {
private static let pi : Float = 3.14159
private static let pi2 : Float = pi / 2.0
static func linear (t : Float) -> Float{
return t
}
static func easeInQuad(t : Float) -> Float{
return t * t
}
static func easeOutQuad(t : Float) -> Float{
return t * (2.0 - t)
}
static func easeInOutQuad(t : Float) -> Float{
if (t < 0.5) {
return 2.0 * t * t
} else {
let f = t - 1.0
return 1.0 - 2.0 * f * f
}
}
static func easeInCubic(t : Float) -> Float{
return t * t * t
}
static func easeOutCubic(t : Float) -> Float{
let f = t - 1.0
return 1.0 + f * f * f
}
static func easeInOutCubic(t : Float) -> Float{
if (t < 0.5) {
return 4.0 * t * t * t
} else {
let f = t - 1.0
return 1.0 + 4.0 * f * f * f
}
}
static func easeInQuart(t : Float) -> Float{
return t * t * t * t
}
static func easeOutQuart(t : Float) -> Float{
let f = t - 1.0
return 1.0 - f * f * f * f
}
static func easeInOutQuart(t : Float) -> Float{
if (t < 0.5) {
return 8.0 * t * t * t * t
} else {
let f = t - 1.0
return 1.0 - 8.0 * f * f * f * f
}
}
static func easeInQuint(t : Float) -> Float{
return t * t * t * t * t
}
static func easeOutQuint(t : Float) -> Float{
let f = t - 1.0
return 1.0 + f * f * f * f * f
}
static func easeInOutQuint(t : Float) -> Float{
if (t < 0.5) {
return 16.0 * t * t * t * t * t
} else {
let f = t - 1.0
return 1.0 + 16.0 * f * f * f * f * f
}
}
static func easeInSine(t : Float) -> Float{
return sinf((t - 1.0) * pi2) + 1.0
}
static func easeOutSine(t : Float) -> Float{
return sinf(t * pi2)
}
static func easeInOutSine(t : Float) -> Float{
return 0.5 * (1.0 - cosf(t * pi))
}
static func easeInCirc(t : Float) -> Float{
return 1.0 - sqrtf(1.0 - t * t)
}
static func easeOutCirc(t : Float) -> Float{
return sqrtf((2.0 - t) * t)
}
static func easeInOutCirc(t : Float) -> Float{
if (t < 0.5) {
return 0.5 * (1.0 - sqrtf(1.0 - 4.0 * t * t))
} else {
return 0.5 * sqrt(-4.0 * t * t + 8.0 * t - 3.0) + 0.5
}
}
static func easeInExpo(t : Float) -> Float{
return (t == 0.0) ? t : powf(2.0, 10.0 * (t - 1.0))
}
static func easeOutExpo(t : Float) -> Float{
return (t == 1.0) ? t : 1.0 - powf(2.0, -10.0 * t)
}
static func easeInOutExpo(t : Float) -> Float{
if (t == 0.0 || t == 1.0) {
return t
} else if (t < 0.5) {
return 0.5 * powf(2.0, 20.0 * t - 10.0)
} else {
return 1.0 - 0.5 * powf(2.0, -20.0 * t + 10.0)
}
}
static func easeInElastic(t : Float) -> Float{
return sin(13.0 * pi2 * t) * powf(2.0, 10.0 * (t - 1.0))
}
static func easeOutElastic(t : Float) -> Float{
return sinf(-13.0 * pi2 * (t + 1.0)) * powf(2.0, -10.0 * t) + 1.0
}
static func easeInOutElastic(t : Float) -> Float{
if (t < 0.5) {
return 0.5 * sinf(13.0 * pi * t) * powf(2.0, 20.0 * t - 10.0)
} else {
return 0.5 * sinf(-13.0 * pi * t) * powf(2.0, -20.0 * t + 10.0) + 1.0
}
}
private static let s : Float = 1.70158
static func easeInBack(t : Float) -> Float{
return ((s + 1.0) * t - s) * t * t
}
static func easeOutBack(t : Float) -> Float{
let f = 1.0 - t
let a = 1.0 - ((s + 1.0) * f - s) * f * f
return a
}
static func easeInOutBack(t : Float) -> Float{
if (t < 0.5) {
let f = 2.0 * t
return 0.5 * ((s + 1.0) * f - s) * f * f
} else {
let f = 2.0 * (1.0 - t)
return 1.0 - 0.5 * ((s + 1.0) * f - s) * f * f
}
}
static func easeInBackExtreme(t : Float) -> Float{
return (t * t - sinf(t * pi)) * t
}
static func easeOutBackExtreme(t : Float) -> Float{
let f = 1.0 - t
return 1.0 - (f * f - sinf(f * pi)) * f
}
static func easeInOutBackExtreme(t : Float) -> Float{
if (t < 0.5) {
let f = 2.0 * t
return 0.5 * (f * f - sinf(f * pi)) * f
} else {
let f = 2.0 * (1.0 - t)
return 1.0 - 0.5 * (f * f - sinf(f * pi)) * f
}
}
static func easeInBounce(t : Float) -> Float{
return 1.0 - easeOutBounce(1.0 - t)
}
static func easeOutBounce(t : Float) -> Float{
let m : Float = 7.5625
if (t < 1.0 / 2.75) {
return m * t * t
} else if (t < 2.0 / 2.75) {
let t1 = t - 1.5 / 2.75
return m * t1 * t1 + 0.75
} else if (t < 2.5 / 2.75) {
let t1 = t - 2.25 / 2.75
return m * t1 * t1 + 0.9375
} else {
let t1 = t - 2.625 / 2.75
return m * t1 * t1 + 0.984375
}
}
static func easeInOutBounce(t : Float) -> Float{
if (t < 0.5) {
return 0.5 * easeInBounce(t * 2.0)
} else {
return 0.5 * easeOutBounce(t * 2.0 - 1.0) + 0.5
}
}
static func smoothStep(t : Float) -> Float{
return t * t * (3 - 2 * t)
}
}
@joelparsons
Copy link
Author

joelparsons commented Mar 4, 2015

Adapted from SKTTimingFunctions.m to work with SKAction.timingFunction

let action = SKAction.scaleTo(2.0, duration:1.0)
action.timingFunction = ActionTimingFunction.easeOutBack

Unfortunately this is broken as actions stop executing as soon as a timing function returns >= 1

Timing functions for SKTEffects. Based on Robert Penner's easing equations
http://robertpenner.com/easing/ and https://github.com/warrenm/AHEasing

Copyright (c) 2013 Razeware LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment