CHCubicBezier 2.0.0

CHCubicBezier 2.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2016
SPMSupports SPM

Maintained by Calvin Huang.




  • By
  • Calvin

CHCubicBezier

About

CubicBezier provides cubic-bezier easing like CSS transition-timing-function cubic-bezier acts. Translated from npm-module bezier-easing and implementation based on this article.

Usage

let cubicBezier = CubicBezier(mX1: 0, mY1: 0, mX2: 1, mY2: 0.5)
cubicBezier.easing(0.0)  // 0
cubicBezier.easing(0.5)  // 0.3125
cubicBezier.easing(1.0)  // 1

or

let cubicBezier = CubicBezier(controlPoints: (0.25, 0.1, 0.25, 0.1))

or

let cubicBezier = CubicBezier(easing: CubicBezier.Easing.ease)
enum rawValue
CubicBezier.Easing.ease (0.25, 0.1, 0.25, 0.1)
CubicBezier.Easing.easeIn (0.42, 0.0, 1.0, 1.0)
CubicBezier.Easing.easeOut (0.0, 0.0, 0.58, 1.0)
CubicBezier.Easing.easeInOut (0.42, 0.0, 0.58, 1.0)
CubicBezier.Easing.linear (0, 0, 1, 1)

You can also get current control points from CubicBezier instance or CubicBezier.easing.

CubicBezier.Easing.ease.toControlPoints() // (0.25, 0.1, 0.25, 0.1)

let cubicBezier = CubicBezier(controlPoints: (0.25, 0.1, 0.25, 0.1))
cubicBezier.controlPoints // // (0.25, 0.1, 0.25, 0.1)

Why CubicBezier instead of CoreAnimation

CAMediaTimingFunction doesn’t provide any ways to calculate value at time(t).

Here is the problem what CubicBezier wants to solve, calculating B(t) value for building customized animation.

Advanced Example

let cubicBezier = CubicBezier(mX1: 0, mY1: 0, mX2: 1, mY2: 0.5)
let duraing: Int = 3  // Seconds
var countingNumber: Int = 0
for time in duraing {
    countingNumber = countingNumber + cubicBezier(time / duraing)
    print("Count: \(countingNumber), At time: \(time)")
}

Install

Via from Swift Package Manager

CHCubicBezier support Swift Package Manager above version 2.0.0.

import PackageDescription

let package = Package(
    name: "MyProject",
    dependencies: [
        .Package(url: "https://github.com/CapsLock-Studio/CHCubicBezier", majorVersion: 2)
    ]
)

Migrate to Objective-C project

Accorading Apple’s document, you need to Update import statements in your Objective-C code (to #import "ProductModuleName-Swift.h").

#import "CHCubicBezier-Swift.h"
// or
@import "CHCubicBezier.h"

All done! Start to use CHCubicBezier!

License

Copyright © Calvin Huang. This software is licensed under the MIT License.