evgenyneu / SpringAnimationCALayer

A helper function for animating CALayer with spring effect in Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Animating CALayer with spring effect in iOS with Swift.

This demo app shows how to do spring animation on CALayer. Animation is similar to UIView's method animateWithDuration(_:delay:usingSpringWithDamping:...).

Usage

  1. Copy SpringAnimation.swift file to your project.
  2. Animate your CALayer's property by calling SpringAnimation.animate function.

For example, let's rotate a button's layer around its X axis in perspective:

let myCALayer = button.layer
var transform = CATransform3DIdentity
transform.m34 = -1.0/100.0
myCALayer.transform = CATransform3DRotate(transform, 0, 1, 0, 0)

SpringAnimation.animate(myCALayer,
                        keypath: "transform.rotation.x",
                        duration: 3.0,
                        usingSpringWithDamping: 0.7,
                        initialSpringVelocity: 1.7,
                        fromValue: M_PI,
                        toValue: 0,
                        onFinished: nil)

Rotate a button around X axis in iOS/Swift

Currently animation looks similar to UIView's method. But the duration, usingSpringWithDamping and initialSpringVelocity values are different from those in UIView's method.

Formula

Here is the formula that I am currently using for animation.

https://www.desmos.com/calculator/pwcg0pepqy

Spring animation formula

The Goal

The goal of this project is to make this CALayer's animation work exactly like UIView's for the same arguments.

Help me, Finn and Jake. You're my only hope.

•ᴥ•

Spring animation for CALayer in iOS with Swift

About

A helper function for animating CALayer with spring effect in Swift


Languages

Language:Swift 100.0%