DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Playing With Interpolation Functions in Swift

Playing With Interpolation Functions in Swift

Follow this short guide to interpolation options for your Swift apps.

Simon Gladman user avatar by
Simon Gladman
·
Feb. 02, 16 · Mobile Zone · Tutorial
Like (1)
Save
Tweet
3.78K Views

Join the DZone community and get the full member experience.

Join For Free

One of the subjects I'll be covering in my upcoming book, Core Image for Swift, is custom transitions and, as part of that, I wanted to add different interpolations for the transition. I found a great post, Interpolation Tricks, which beautifully explains different interpolation functions and I thought it would be nice to port some of that code to Swift and create a handy playground to demonstrate them.

So, here for your enjoyment is my Interpolation Playground.

All the different interpolations use a single loop function to draw the graph. This function accepts the interpolation function as an argument which is of type (Double) -> Double. 

func loop( interpolationFunction: Double -> Double )
{
    let n = 50

    for i in 0 ..< n
    {
        let v = Double(i) / Double(n)

        let _ = interpolationFunction(v)
    }

}

The first example is linear interpolation where the call is simply 

        loop { x in x }

Next is smoothstep where the call is:

        loop { x in ((x) * (x) * (3 - 2 * (x))) }

Then onto smootherstep where the call is:

        loop { x in ((x) * (x) * (x) * ((x) * ((x) * 6 - 15) + 10)) }




Squared and inverse squared are pretty much as expected:


        loop { x in (x * x) }

        loop { x in 1 - (1 - x) * (1 - x) }

Now we start to get funky with Catmull-Rom interpolation - try playing with the q and t values in the playground!


        func catmullRom(t: Double, _ p0: Double, _ p1: Double, _ p2: Double, _ p3: Double) -> Double
        {
            return 0.5 * (
                (2 * p1) +
                (-p0 + p2) * t +
                (2 * p0 - 5 * p1 + 4 * p2 - p3) * t * t +
                (-p0 + 3 * p1 - 3 * p2 + p3) * t * t * t
            )
        }

        let q = -15.0
        let t = 7.5

        loop { x in catmullRom(x, q, 0.0, 1.0, t) }

There are also two elastic interpolations - in and out. The original code comes from Robert Penner in this post with a refactor from Josh on Design in this post.

For elastic ease in, the code is:

 func elasticIn(x: Double, p: Double = 0.2) -> Double
        {
            return pow(2, 10 * (x - 1)) * sin((x - p/4) * (2 * M_PI) / p) + 1
        }

        loop { x in elasticIn(x) }

Ease out is almost the same code:

func elasticOut(x: Double, p: Double = 0.2) -> Double
        {
            return pow(2, -10 * x) * sin((x - p/4) * (2 * M_PI) / p) + 1
        }

        loop { x in elasticOut(x) }




This playground is available at my GitHub repository here. Xcode doesn't seem to support source control for playgrounds, so you'll need to download the zip file. 

For additional functions, Robert Penner's Easing.as ActionScript code has loads of great examples and is available at his repo here.

Interpolation Swift (programming language)

Published at DZone with permission of Simon Gladman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java: Why Core-to-Core Latency Matters
  • SQL CTE: How to Master It in One Sitting With Easy Examples
  • How Template Literal Types Work in TypeScript
  • Getting Started Building on the NEAR Network with Infura

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo