Physics-based animations for iOS, tvOS, and macOS.

Overview

Build Status Swift 5.0 GitHub release SwiftPM compatible CocoaPods compatible

Advance

An animation library for iOS, tvOS, and macOS that uses physics-based animations (including springs) to power interactions that move and respond realistically.

let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

// Springs animate changes to a value
let spring = Spring(initialValue: view.center)

// The `onChange` closure will be called every time the spring updates
spring.onChange = { [view] newCenter in
    view.center = newCenter
}

/// The view's center will realistically animate to the new target value.
spring.target = CGPoint(x: 300, y: 200)

Installation

There are several ways to integrate Advance into your project.

  • Manually: add Advance.xcodeproj to your project, then add Advance-{iOS|macOS|tvOS}.framework as an "Embedded Binary" to your application target (under General in target settings). From there, add import Advance to your code and you're good to go.

  • Carthage: add github "timdonnelly/Advance" to your Cartfile.

  • CocoaPods: add pod 'Advance' to your Podfile.

  • Swift Package Manager: add a dependency to your Project.swift: .package(url: "http://github.com/timdonnelly/Advance", from: "3.0.0")

Requirements
  • iOS 10+, tvOS 10+, or macOS 10.12+
  • Swift 5.0 (Xcode 10.2 or higher)

Usage

API documentation is available here.

Advance animations are applied on every frame (using CADisplayLink on iOS/tvOS, and CVDisplayLink on macOS), allowing for fine-grained control at any time.

Spring

Spring instances animate changes to a value over time, using spring physics.

let spring = Spring(initialValue: 0.0)
spring.onChange = { [view] newAlpha in 
    view.alpha = newAlpha 
}

// Off it goes!
spring.target = 0.5

Configuring a spring

/// Spring values can be adjusted at any time.
spring.tension = 30.0 /// The strength of the spring
spring.damping = 2.0 /// The resistance (drag) that the spring encounters
spring.threshold = 0.1 /// The maximum delta between the current value and the spring's target (for each component) for which the simulation can enter a converged state.

/// Update the simulation state at any time.
spring.velocity = 6.5
spring.value = 0.2

/// Sets the spring's target and the current simulation value, and removes all velocity. This causes the spring to converge at the given value.
spring.reset(to: 0.5)

Animator

Animator allows for more flexibility in the types of animation that can be performed, but gives up some convenience in order to do so. Specifically, animators allow for any type of animation or simulation to be performed for a single value.

let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

/// Animators coordinate animations to drive changes to a value.
let sizeAnimator = Animator(initialValue: view.bounds.size)

sizeAnimator.onChange = { [view] newSize in
    view.bounds.size = newSize
}

/// A simple timed animation
sizeAnimator.animate(to: CGSize(width: 123, height: 456), duration: 0.25, timingFunction: .easeInOut)

/// Some time in the future (before the previous timed animation was complete)...

/// Spring physics will move the view's size to the new value, maintaining the velocity from the timed animation.
sizeAnimator.simulate(using: SpringFunction(target: CGSize(width: 300, height: 300)))

/// Some time in the future (before the previous spring animation was complete)...

/// The value will keep the same velocity that it had from the preceeding spring
/// animation, and a decay function will slowly bring movement to a stop.
sizeAnimator.simulate(using: DecayFunction(drag: 2.0))

Animators support two fundamentally different types of animations: timed and simulated.

Timed animations

Timed animations are, well, timed: they have a fixed duration, and they animate to a final value in a predictable manner.

animator.animate(to: CGSize(width: 123, height: 456), duration: 0.25, timingFunction: .easeInOut)

TimingFunction described the pacing of a timed animation.

TimingFunction comes with a standard set of functions:

TimingFunction.linear // No easing
TimingFunction.easeIn
TimingFunction.easeOut
TimingFunction.easeInOut
TimingFunction.swiftOut // Similar to Material Design's default curve

Custom timing functions can be expressed as unit beziers (described here).

let customTimingFunction = TimingFunction(x1: 0.1, y1: 0.2, x2: 0.6, y2: 0.0)

Simulated animations

Simulated animations use a simulation function to power a physics-based transition. Simulation functions are types conforming to the SimulationFunction protocol.

Simulated animations may be started using two different methods:

// Begins animating with the custom simulation function, maintaining the previous velocity of the animator.
animator.simulate(using: MyCustomFunction())

// or...

// Begins animating with the custom simulation function, imparting the specified velocity into the simulation.
animator.simulate(using: DecayFunction(), initialVelocity: dragGestureRecognizer.velocity(in: view))

Animating Custom Types

Values conforming to the VectorConvertible protocol can be animated by Advance. Conforming types can be converted to and from a Vector implementation.

public protocol VectorConvertible: Equatable, Interpolatable {
    associatedtype VectorType: SIMD where VectorType.Scalar == Double
    init(vector: VectorType)
    var vector: VectorType { get }
}

The library adds conformance for many common types through extensions.

Contributing

If you encounter any issues or surprises, please open an issue.

For suggestions or new features, please consider opening a PR with a functional implementation. Issues may be used if you aren't sure how to implement the change, but working code is typically easier to evaluate.

License

This project is released under the BSD 2-clause license.

Comments
  • Tags should not be prepended with 'v', fails SPM

    Tags should not be prepended with 'v', fails SPM

    Hi,

    Xcode 11 finally has SPM support built-in, so adding SPM packages to iOS / macOS is now possible without much hassle.

    The current Package.swift works fine for this, there is only one issue, Xcode cannot find version 3.0.0 because it's tagged as v3.0.0 in the releases/tags.

    The only way to add the framework now is by selecting the master branch, ideally not what you'd want.

    Can version v3.0.0 be tagged again as 3.0.0 and further versions from here on out? So we can use the 'power' of semver.

    Thanks!

    opened by terwanerik 4
  • Add CocoaPods support

    Add CocoaPods support

    Hey, this pull request would close #2

    • [x] create podspec
    • [x] podspec passes lint
    dkhamsing$ pod --version
    0.39.0
    
    dkhamsing$ pod spec lint Advance.podspec 
    
     -> Advance (0.9)
    
    Analyzed 1 podspec.
    
    Advance.podspec passed validation.
    
    • [x] integrate in simple demo https://github.com/dkhamsing/Advance/tree/cocoapods-demo/Cocoapods-demo
    • [x] get setup with CocoaPods Trunk service (to publish/make it public) @timdonnelly https://guides.cocoapods.org/making/getting-setup-with-trunk.html
    • [x] update readme @timdonnelly

    To install using CocoaPods, add the following to your Podfile:

    pod 'Advance', '~> 0.9'
    
    opened by dkhamsing 3
  • Carthage won't build for macOS

    Carthage won't build for macOS

    When trying to build for macOS with Carthage, I get the error Dependency "Advance" has no shared framework schemes for any of the platforms: Mac.

    The main GitHub page indicates Advance is compatible with macOS. Is that not yet implemented or is there some way I can make it work?

    I'm using Xcode 10.1, macOS 10.13.6.

    opened by benstahl 2
  • Installation with carthage failed

    Installation with carthage failed

    I don't know what to do next. Any suggestion?

    *** Building scheme "Advance-tvOS" in Advance.xcodeproj
    xcodebuild: error: The run destination Apple TV 1080p is not valid for Running the scheme 'Advance-tvOS'.
        Apple TV 1080p doesn’t match any of Advance.framework’s targeted device families. You can expand Advance.framework’s targeted device families to support Apple TV 1080p.
    A shell task failed with exit code 70:
    xcodebuild: error: The run destination Apple TV 1080p is not valid for Running the scheme 'Advance-tvOS'.
        Apple TV 1080p doesn’t match any of Advance.framework’s targeted device families. You can expand Advance.framework’s targeted device families to support Apple TV 1080p.
    
    opened by liweiz 2
  • Team ID in project

    Team ID in project

    Disclaimer: Carthage newbie here, but I created the Cartfile and did:

    carthage update --platform iOS
    *** Fetching Advance
    *** Checking out Advance at "0.9"
    *** xcodebuild output can be found in /var/folders/_l/d2wy_rhd1kg7h_rz74px4hmh0000gn/T/carthage-xcodebuild.64oimv.log
    *** Building scheme "Advance" in Advance.xcodeproj
    ** BUILD FAILED **
    
    
    The following build commands failed:
        Check dependencies
    (1 failure)
    Code Sign error: No code signing identities found: No valid signing identities (i.e. certificate and private key pair) matching the team ID “HV4J834MC8” were found.
    A shell task failed with exit code 65:
    ** BUILD FAILED **
    
    
    The following build commands failed:
        Check dependencies
    (1 failure)
    

    is something that you can do or is it at Carthage level?

    opened by ecarnevale 2
  • What is AnimatablePair in SimpleTransform.swift?

    What is AnimatablePair in SimpleTransform.swift?

    I download and build the SampleApp-iOS, show me some errors.

    1. Type 'SimpleTransform' does not conform to protocol 'VectorConvertible'
    2. Use of undeclared type 'AnimatablePair'

    The environment is macOS 10.15.5 XCode 11.5 iOS Simulator 13.5 SimpleTransform_bug_01

    Please help me, what is AnimatablePair?

    opened by ArchimboldiMao 1
  • Example app crashes on Gestures screen 2 fingers tap

    Example app crashes on Gestures screen 2 fingers tap

    Steps to reproduce:

    1. Launch AdvanceSample app.
    2. Go to Gestures panel (last one).
    3. Tap with 2 fingers on Gesture panel, app crashes.

    Update: As a matter of fact app crashes on 2-fingers tap on any panel, not only the Gestures one. Tested in iPhone 6 Plus, iOS 9.2.1.

    opened by Auralien 1
  • Please add the option to animate buttons made with NGraphics

    Please add the option to animate buttons made with NGraphics

    Great library! I'm using the popular NGraphics lib (excellent for creating UI on iOS because it can render SVG vector images, and you can draw UI controls in Inkscape in minutes instead of coding them manually for days). So I want to make an important feature request: can you add the option to animate vector buttons made with the NGraphics library? Here is the link if you don't know it already:

    https://github.com/praeclarum/NGraphics

    For example it would be cool to:

    • interpolate between a "pushed" state and a "popped" state of a custom button made with NGraphics
    • interpolate between an "off" state and an "on" state in a custom switch made with NGraphics
    • interpolate between an "expanded" state and a "minimized" state of a box with NGraphics borders
    • interpolate between the 1% and the 100% of a progress bar/slider controller made with NGraphics
    • and so on..

    It would be a marriage made in heaven!

    Thanks in advance!

    opened by Emasoft 1
  • codebeat badge

    codebeat badge

    Is it fine to add codebeat badge to README?

    codebeat is automated code review tool for Swift, Ruby & Go that helps get instant feedback on code quality.

    "Quick wins" suggested by codebeat could be a nice candidate for a pull request and help other developers become contributors.

    FYI. To be fully open and honest. I'm co-founder of codebeat.

    opened by korzonek 1
  • Spring animation (or any other) won't start after setting target.

    Spring animation (or any other) won't start after setting target.

    HI, I've been trying for hours to make the animation start, but couldn't. The onChange method, never gets called. This is the code:

    let spring = Spring(initialValue: 0)
    spring.onChange = { [weak view] val in
              print("change!") // never gets called.
              view?.frame.origin.y = val
    }
    
    spring.target = 500
    

    I've tried debugging the whole thing and the simulation function never turns into .animating (state). I can't figure this out. Tried calling both view.setNeedsLayout() and view.setNeedsDisplay().

    What am I missing? Thanks.

    opened by TomerAvni 0
Releases(3.1.0)
  • 3.1.0(Oct 18, 2019)

    This release contains some internal cleanup, but the big change is the addition of a proper Package.swift for use with Xcode 11 (until Xcode received proper package manager support, there was little reason to support it for a UI-focused library)

    Source code(tar.gz)
    Source code(zip)
  • v3.0.0(Apr 19, 2019)

  • v2.2.0(Feb 2, 2019)

  • v2.1.0(Sep 25, 2018)

    This release also moves the sample apps into a SampleApp/ directory, which now contains a Gemfile + Podfile – because Cocoapods is now used to integrate the framework with the sample apps! This really cleans up the top-level directory structure.

    Source code(tar.gz)
    Source code(zip)
  • v2.0.0.alpha3(Feb 20, 2018)

  • v2.0.0.alpha2(Feb 20, 2018)

    • Removed a few cases of redundant type constraints that were causing warnings.
    • Wrapped UIKit-specific extensions in macros to fix Cocoapods release for macOS.
    Source code(tar.gz)
    Source code(zip)
  • v2.0.0.alpha1(Feb 20, 2018)

  • v1.0.0(Feb 14, 2018)

  • v1.0.0.beta1(Jan 2, 2018)

    ...because that means I can move on to 2.0!

    1.0 is a small release that is mainly focused on cleaning up stale code. This project was started in the Swift 1/2 days, and it needs some attention to clean out the 'vintage' bits. The worst offenders have been taken care of, so I'm going to go ahead and call this a 1.0 release.

    I have some ideas for a 2.0 release that should be much more cohesive – the project in its current state was lifted ad-hoc out of a shipping app. It has some good parts, but it's hard to know how to use it out of the box.

    Stay tuned.

    Source code(tar.gz)
    Source code(zip)
  • 0.9.3(Jan 1, 2018)

  • 0.9.2(Dec 22, 2016)

    Thanks @steadicat!

    Note that Xcode may complain about legacy Swift code that needs to be converted – this is just coming from the Playground included in the project. I'll clean those up at some point, but I don't want to delay releasing this until I find the time to do so.

    Source code(tar.gz)
    Source code(zip)
  • 0.9.1(Mar 22, 2016)

  • 0.9(Mar 22, 2016)

YapAnimator is your fast and friendly physics-based animation system

YapAnimator is your fast and friendly physics-based animation system. YapAnimator was built with ease-of-use in mind, keeping you sane and your design

Yap Studios 1.9k Dec 6, 2022
A Swift library to take the power of UIView.animateWithDuration(_:, animations:...) to a whole new level - layers, springs, chain-able animations and mixing view and layer animations together!

ver 2.0 NB! Breaking changes in 2.0 - due to a lot of requests EasyAnimation does NOT automatically install itself when imported. You need to enable i

Marin Todorov 3k Dec 27, 2022
This repo contains swift collection of gui, games, menu, animations, music, payment, etc... for iOS, macOS, watchOS and tvOS

Swift-Collections About: This repo contains a collection of projects built using swift and objective-c Contains projects for macOS iOS iPad watchOS tv

Krisna Pranav 6 Nov 15, 2022
(Animate CSS) animations for iOS. An easy to use library of iOS animations. As easy to use as an easy thing.

wobbly See Wobbly in action (examples) Add a drop of honey ?? to your project wobbly has a bunch of cool, fun, and easy to use iOS animations for you

Sagaya Abdulhafeez 150 Dec 23, 2021
(Animate CSS) animations for iOS. An easy to use library of iOS animations. As easy to use as an easy thing.

wobbly See Wobbly in action (examples) Add a drop of honey ?? to your project wobbly has a bunch of cool, fun, and easy to use iOS animations for you

Sagaya Abdulhafeez 150 Dec 23, 2021
A collection of animations for iOS. Simple, just add water animations.

DCAnimationKit A collection of animations for iOS Simply, just add water! DCAnimationKit is a category on UIView to make animations easy to perform. E

Dalton 797 Sep 23, 2022
Wave is a spring-based animation engine for iOS that makes it easy to create fluid, interruptible animations that feel great.

Wave is a spring-based animation engine for iOS and iPadOS. It makes it easy to create fluid, interactive, and interruptible animations that feel great.

Janum Trivedi 1.2k Jan 8, 2023
Advanced Natural Motion Animations, Simple Blocks Based Syntax

FlightAnimator Moved to Swift 3.1 Support: For Swift 3.1 - Use tag Version 0.9.9 See Installation Instructions for clarification Introduction FlightAn

Anton 589 Dec 29, 2022
SamuraiTransition is an open source Swift based library providing a collection of ViewController transitions featuring a number of neat “cutting” animations.

SamuraiTransiton is a ViewController transition framework in Swift. It is an animation as if Samurai cut out the screen with a sword. transition types

hachinobu 273 Dec 29, 2022
A port of SwiftUILab's Advanced Animations that also supports macOS

SwiftUILab Advanced Animations on the Mac as well A port of SwiftUILab's Advanced Animations that also supports macOS Here's the Ghist of the original

Mihaela Mihaljevic Jakic 10 Jan 2, 2023
Swift animation library for iOS, tvOS and macOS.

anim is an animation library written in Swift with a simple, declarative API in mind. // moves box to 100,100 with default settings anim { self.bo

Onur Ersel 555 Dec 12, 2022
Easy to read and write chainable animations in Objective-C and Swift

Whats new in version 3.x? Swiftier syntax Swift 4 support Bug fixes and improvements Whats new in version 2.x? Re-architected from the ground up, no m

Jeff Hurray 3.2k Dec 30, 2022
⛓ Easy to Read and Write Multi-chain Animations Lib in Objective-C and Swift.

中文介绍 This project is inspired by JHChainableAnimations! Why Choose LSAnimator & CoreAnimator? You can write complex and easy-to-maintain animations in

木子 1.6k Nov 22, 2022
A library of custom iOS View Controller Animations and Interactions.

RZTransitions is a library to help make iOS7 custom View Controller transitions slick and simple. Installation CocoaPods (Recommended) Add the followi

Rightpoint 1.9k Nov 20, 2022
Match animations in SwiftUI and UIKit/AppKit

MatchedAnimation Match animations in SwiftUI and UIKit/AppKit. /// Draw a box in

Ryan Carver 6 Dec 21, 2022
Tools for SwiftUI that helps perform Path and Shape animations, such us morphing circle or shape transformations

SwiftUI+PathAnimations ?? Introduction This packages contains SimilarShape and InterpolatedShape, both can be used to achieve shapes animations with S

Alfredo Delli Bovi 180 Dec 29, 2022
Easily pause and resume SwiftUI animations

swiftui-pausable-animation Easily pause and resume SwiftUI animations! Installation This component is distributed as a Swift package. Just add this re

Gordan Glavaš 6 Nov 24, 2022
Simple UIButton subclass with additional state change animations (e.g. backgroundColor) and missing features

SimpleButton UIButton subclass with animated, state-aware attributes. Easy to subclass and configure! Full API docs Usage Just create your own SimpleB

Andreas Tinoco Lobo 169 Sep 14, 2022