返回排行榜

jtrivedi/Wave

Swiftjtrivedi.github.io/Wave/

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

animationgesturesiosmotionswiftinteraction-designuikituiappkitswiftui
Star 增长趋势
Star
2.4k
Forks
72
周增长
Issues
11
1k2k
2022年5月2023年9月2025年2月2026年7月
README

Wave

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

Wave has no external dependencies, and can be easily dropped into existing UIKit, SwiftUI, or AppKit based projects and apps.

The core feature of Wave is that all animations are re-targetable, meaning that you can change an animation’s destination value in-flight, and the animation will gracefully redirect to that new value.

Understanding Retargeting

Consider these demos of the iOS Picture-in-Picture feature. The screen on the left is created with standard UIKit animations, and the one on the right is created with Wave.

Though both are “interruptible”, the Wave-based implementation handles the interruption much better, and fluidly arcs to its new destination. The UIKit animation feels stiff and jerky in comparison.

At its core, retargeting is the process of preserving an animation’s velocity even as its target changes, which Wave does automatically.

Demo

Installation

Add Wave to your app's Package.swift file, or selecting File -> Add Packages in Xcode:

.package(url: "https://github.com/jtrivedi/Wave")

If you clone the repo, you can run the sample app, which contains a few interactive demos to understand what Wave provides.

Note: To enable high frame-rate animations on ProMotion devices (i.e. 120 fps animation), you'll need to add a key/value pair in your Info.plist. Set the key CADisableMinimumFrameDuration to true. Without this entry, animations will be capped at 60 fps.

Documentation

There’s a full Wave documentation site available for full API and usage documentation.

Getting Started

There are two ways you can interact with Wave, depending on your needs: the block-based and property-based animations:

Block-Based Animation

The easiest way to get started is by using Wave’s block-based APIs that resemble the UIView.animateWithDuration() APIs.

This API lets you animate several common UIView and CALayer properties, like frame, center, scale, backgroundColor, and more.

For these supported properties, Wave will create, manage, and execute the required spring animations under-the-hood.

For example, animating the above PiP view to its final destination is extremely simple:

if panGestureRecognizer.state == .ended {

    // Create a spring with some bounciness. `response` affects the animation's duration.
    let animatedSpring = Spring(dampingRatio: 0.68, response: 0.80)

    // Get the gesture's lift-off velocity, and pass it into the Wave animation.
    let gestureVelocity = panGestureRecognizer.velocity(in: view)

    Wave.animate(withSpring: animatedSpring, gestureVelocity: gestureVelocity) {
        // Update animatable properties on the view's `animator` property, _not_ the view itself.
        pipView.animator.center = pipViewDestination     // Some target CGPoint that you calculate.
        pipView.animator.scale = CGPoint(x: 1.1, y: 1.1)
    }
}

Note that at any time, you can retarget the view’s center property to somewhere else, and it’ll gracefully animate.

Supported Animatable Properties

The block-based API currently supports animating the following properties. For other properties, you can use the property-based animation API below.

  • frame
  • bounds
  • center
  • origin
  • alpha
  • backgroundColor
  • cornerRadius
  • scale
  • translation
  • shadowColor/radius/offset/opacity
  • borderColor/borderWidth

Upcoming properties:

  • rotation

Property-Based Animation

While the block-based API is often most convenient, you may want to animate something that the block-based API doesn’t yet support (e.x. rotation). Or, you may want the flexibility of getting the intermediate spring values and driving an animation yourself (e.x. a progress value).

For example, to draw the orange path of the PiP demo, we need to know the value of every CGPoint from the view’s initial center, to its destination center:

// When the gesture ends, create a `CGPoint` animator from the PiP view's initial center, to its target.
// The `valueChanged` callback provides the intermediate locations of the callback, allowing us to draw the path.

let positionAnimator = SpringAnimator<CGPoint>(spring: animatedSpring)
positionAnimator.value = pipView.center       // The presentation value
positionAnimator.target = pipViewDestination  // The target value
positionAnimator.velocity = gestureVelocity

positionAnimator.valueChanged = { [weak self] location in
    self?.drawPathPoint(at: location)
}

positionAnimator.start()
Completion Blocks

Both the block-based and property-based APIs support completion blocks. If an animation completes fully, the completion block’s finished flag will be true.

However, if an animation’s target was changed in-flight (“retargeted”), finished will be false, while retargeted will be true.

Wave.animate(withSpring: Spring.defaultAnimated) {
    myView.animator.backgroundColor = .systemBlue
} completion: { finished, retargeted in
    print(finished, retargeted)
}

Example Code

Exploring the provided sample app is a great way to get started with Wave.

Simply open the Wave-Sample Xcode project and hit “Run”. The full source code for the Picture-in-Picture demo is available there, too!

Acknowledgements

Special thanks to Ben Oztalay for helping architect the underlying physics of Wave!

相关仓库
3b1b/manim

Animation engine for explanatory math videos

PythonPyPIMIT Licensepythonanimation
88.8k7.4k
animate-css/animate.css

🍿 A cross-browser library of CSS animations. As easy to use as an easy thing.

CSSnpmOthercss-animationscss
animate.style
82.7k16k
MisterBooo/LeetCodeAnimation

Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路,完整单步/回看/变速/语音讲解在 algomooc.com)

JavaMavenleetcodeleetcode-solutions
algomooc.com
76.6k13.9k
juliangarnier/anime

JavaScript animation engine

JavaScriptnpmMIT Licenseanimationanime
animejs.com
71.3k4.8k
algorithm-visualizer/algorithm-visualizer

:fireworks:Interactive Online Platform that Visualizes Algorithms from Code

JavaScriptnpmMIT Licensealgorithmdata-structure
algorithm-visualizer.org
48.7k7.6k
aseprite/aseprite

Animated sprite editor & pixel art tool (Windows, macOS, Linux)

C++animationpixel-art
aseprite.org
38.2k8.4k
heygen-com/hyperframes

Write HTML. Render video. Built for agents.

TypeScriptnpmApache License 2.0aianimation
36.7k3.5k
airbnb/lottie-android

Render After Effects animations natively on Android and iOS, Web, and React Native

JavaMavenApache License 2.0animationandroid
airbnb.io/lottie/
35.7k5.4k
motiondivision/motion

A modern animation library for React and JavaScript

TypeScriptnpmMIT Licenseanimationanimation-js
motion.dev
32.9k1.3k
pmndrs/react-three-fiber

🇨🇭 A React renderer for Three.js

TypeScriptnpmMIT Licensereactthreejs
docs.pmnd.rs/react-three-fiber
31.5k1.9k
AtsushiSakai/PythonRobotics

Python sample codes and textbook for robotics algorithms.

PythonPyPIOtherpythonrobotics
atsushisakai.github.io/PythonRobotics/
30.1k7.4k
greensock/GSAP

GSAP (GreenSock Animation Platform), a JavaScript animation library for the modern web

JavaScriptnpmanimationgsap
gsap.com
26.9k2.1k