Coder Social home page Coder Social logo

interpolate's Introduction

Interpolate - Swift interpolation for gesture-driven animations

Build Status Version Carthage compatible

Interpolate

Interpolate is a powerful Swift interpolation framework for creating interactive gesture-driven animations.

Usage

The ๐Ÿ”‘ idea of Interpolate is - all animation is the interpolation of values over time.

To use Interpolate:

Import Interpolate at the top of your Swift file.

import Interpolate

Create an Interpolate object with a from value, a to value and an apply closure that applies the interpolation's result to the target object.

let colorChange = Interpolate(from: UIColor.whiteColor(),
to: UIColor.redColor(),
apply: { [weak self] (result) in
    if let color = result as? UIColor {
      self?.view.backgroundColor = color
    }
})

Next, you will need to define a way to translate your chosen gesture's progress to a percentage value (i.e. a CGFloat between 0.0 and 1.0).

For a gesture recognizer or delegate that reports every step of its progress (e.g. UIPanGestureRecognizer or a ScrollViewDidScroll) you can just apply the percentage directly to the Interpolate object:

@IBAction func handlePan(recognizer: UIPanGestureRecognizer) {
    let translation = recognizer.translationInView(self.view)
    let translatedCenterY = view.center.y + translation.y
    let progress = translatedCenterY / self.view.bounds.size.height
    colorChange.progress = progress
}

For other types of gesture recognizers that only report a beginning and an end (e.g. a UILongPressGestureRecognizer), you can animate directly to a target progress value with a given duration. For example:

@IBAction func handleLongPress(recognizer: UILongPressGestureRecognizer) {
    switch recognizer.state {
        case .Began:
            colorChange.animate(1.0, duration: 0.3)
        case .Cancelled, .Ended, .Failed:
            colorChange.animate(0.0, duration: 0.3)
        default: break
    }
}

To stop an animation:

colorChange.stopAnimation()

When you are done with the interpolation altogether:

colorChange.invalidate()

Voila!

What can I interpolate?

Interpolate currently supports the interpolation of:

  • CGPoint
  • CGRect
  • CGSize
  • Double
  • CGFloat
  • Int
  • NSNumber
  • UIColor
  • CGAffineTransform
  • CATransform3D

More types will be added over time.

Advanced usage

Interpolate is not just for dull linear interpolations.

For smoother animations, consider using any of the following functions: EaseIn, EaseOut, EaseInOut and Spring.

// Spring interpolation
let shadowPosition = Interpolate(from: -shadowView.frame.size.width,
to: (self.view.bounds.size.width - shadowView.frame.size.width)/2,
function: SpringInterpolation(damping: 30.0, velocity: 0.0, mass: 1.0, stiffness: 100.0),
apply: { [weak self] (result) in
  if let originX = result as? CGFloat {
      self?.shadowView.frame.origin.x = originX
  }
})

// Ease out interpolation
let groundPosition = Interpolate(from: CGPointMake(0, self.view.bounds.size.height),
to: CGPointMake(0, self.view.bounds.size.height - 150),
function: BasicInterpolation.EaseOut,
apply: { [weak self] (result) in
    if let origin = result as? CGPoint {
        self?.groundView.frame.origin = origin
    }
})

In fact, you can easily create and use your own interpolation function - all you need is an object that conforms to the InterpolationFunction protocol.

Setting up with CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
pod 'Interpolate', '~> 0.1'

Setting up with Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Interpolate into your Xcode project using Carthage, specify it in your Cartfile:

github "marmelroy/Interpolate"

Inspiration

interpolate's People

Contributors

delba avatar marmelroy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.