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.
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)
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.
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)")
}
Via from CocoaPods.
Add pod 'CHCubicBezier'
to your Podfile and run pod install
platform :ios, '8.0'
target 'MyApp' do
# Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
pod 'CHCubicBezier'
target 'MyAppTests' do
inherit! :search_paths
# Pods for testing
end
target 'MyAppUITests' do
inherit! :search_paths
# Pods for testing
end
end
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '3.0'
config.build_settings['MACOSX_DEPLOYMENT_TARGET'] = '10.10'
end
end
end
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)
]
)
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!
Copyright (c) Calvin Huang. This software is licensed under the MIT License.