Blog
Composable animations in Swift.Installation
Cocoapods
The easiest way to get started is to use CocoaPods. Just add the following line to your Podfile:
pod 'SwiftyAnimate', '~> 1.3.0'
Carthage
github "rchatham/SwiftyAnimate"
Swift Package Manager
Add the following line to your Package.swift file.
.Package(url: "https://github.com/rchatham/SwiftyAnimate.git", majorVersion: 0)
Usage
This library can be used to design composable animations while keeping animation code readable and maintainable.
Composing Animations
Compose animations and insert logic inbetween them using the then
, do
, and wait
functions.
Then blocks
Add animations to the current instance using one of the implementations for this function. There are implemetations for spring and keyframe animations as well as chaining Animate
objects together.
Animate(duration: 1.0) {
// animation code goes here
}
.then(duration: 0.5) {
// more animation code
}
.perform()
And blocks
Add animations to the current instance using one of the implementations for this function. There are implemetations for spring and keyframe animations as well as stacking Animate
objects together. 'And' animations are performed in sync with the animation before it.
Animate(duration: 1.0) {
// animation code goes here
}
.and(duration: 0.5) {
// more animation code
}
.perform()
Do blocks
Add code that you don't intend on animating but would like to perform between animations here. Any code you put here will NOT be animated.
Animate(duration: 1.0) {
// animation code goes here
}
.do {
// logic you don't want to animate
}
.then(duration: 0.5) {
// more animation code
}
.perform()
Wait blocks
Add code that you may want to pause an ongoing chain of animations for. Any code you put here will NOT be animated. You can pass in a timeout if you want to wait for a specific amount of time or if you don't want to wait longer to execute the code in the wait block.
Animate(duration: 1.0) {
// animation code goes here
}
.wait(timeout: 5.0) { resume in
// logic you want to pause an animation to complete
resume()
}
.then(duration: 0.5) {
// more animation code
}
.perform()
Performing Animations
There are two ways to perform animations finish
and perform
. Important: You must either call one of these two functions or decay
on an animation instance or this will result in a memory leak!
Perform
This one is easy. Call this on an animation instance to perform it. Perform takes an optional closure which gets excecuted on completing the last animation block.
let animation = Animate(duration: 1.0) {
// animations
}
animation.perform()
Finish
If you don't need to pass in a completion closure try calling finish on your animation instance. The animation passed in is enqueue'd and then perform is called on the instance. Finish has all of the same variations as the then function.
Animate(duration: 1.0) {
// animations
}
.finish(duration: 1.0) {
// animations
}
Decay
If you would like to deallocate an animation instance without performing it call decay on it.
let animation = Animate(duration: 1.0) {
// animations
}
animation.decay()
UIView Extensions
A number of animatable properties have extensions defined to make implementing them with this library even easier. Please check the docs!
Best Practices
The best way to take advantage of this library is define extensions for the views that you would like to animate. This compartmentailizes your code and keep all the animation logic tucked up within the view and out of your view controllers.
extension AnimatingView {
func bounceAnimation() -> Animate {
return Animate()
.then(animation: scale(duration: 0.3, x: 1.3, y: 1.3))
.then(animation: scale(duration: 0.3, x: 0.8, y: 0.8))
.then(animation: scale(duration: 0.3, x: 1.1, y: 1.1))
.then(animation: scale(duration: 0.3, x: 1.0, y: 1.0))
}
}
Then when you go to perform an animation you just have to call perform()
on the returned animation.
let animatingView = AnimatingView()
animatingView.bounceAnimation().perform()
And string them together with other animations for building up complex animation logic easily.
Animate()
.then(animation: animatingView.bounceAnimation())
.then(animation: animatingView.tiltAnimation())
.then(animation: animatingView.bounceAnimation())
.perform()
Contributing
I would love to see your ideas for improving this library! The best way to contribute is by submitting a pull request. I'll do my best to respond to your patch as soon as possible. You can also submit a new GitHub issue if you find bugs or have questions.
Please make sure to follow our general coding style and add test coverage for new features!