Swiftui-animation-observer - Track SwiftUI animation progress and completion via callbacks

Overview

SwiftUI Animation Observer

Track SwiftUI animation progress and completion via callbacks! For an animated value (offset, opacity, etc.), get its current value as the animation progresses and then get notified when the animation is completed.

Preview

Installation

This component is distributed as a Swift package. Just add this repo's URL to XCode:

https://github.com/globulus/swiftui-animation-observer

How to use

Add the animationObserver modifier to your view. Pass the property whose value is changed by the animation and two optional callbacks:

  1. onProgress is called on each animation frame and reports the current value of the animated property.
  2. onComplete is called when the animation is completed.
struct AnimationObserverTest: View {
  @State private var offset = 0.0
  @State private var offsetSpan: ClosedRange<Double> = 0...1
  @State private var progressPercentage = 0.0
  @State private var isDone = false

   var body: some View {
     GeometryReader { geo in
       VStack {
         Text("Loading: \(progressPercentage)%")
         Rectangle()
           .foregroundColor(.blue)
           .frame(height: 50)
           .offset(x: offset)
           .animationObserver(for: offset) { progress in // HERE
             progressPercentage = 100 * abs(progress - offsetSpan.lowerBound) / (offsetSpan.upperBound - offsetSpan.lowerBound)
           } onComplete: {
             isDone = true
           }

         if isDone {
           Text("Done!")
         } else if progressPercentage >= 50 {
           Text("Woooooah, we're half way there...")
         }
         
         Button("Reload") {
           isDone = false
           offset = -geo.size.width
           offsetSpan = offset...0
           withAnimation(.easeIn(duration: 5)) {
             offset = 0
           }
         }
       }
     }
   }
}

Recipe

Check out this recipe for in-depth description of the component and its code. Check out SwiftUIRecipes.com for more SwiftUI recipes!

Changelog

  • 1.0.0 - Initial release.
You might also like...
ScriptLib - Early and in progress scripting suite for Swift apps

ScriptLib Early and in progress scripting suite for Swift apps. The goal is to c

Simple Interface Core Animation. Run type-safe animation sequencially or parallelly
Simple Interface Core Animation. Run type-safe animation sequencially or parallelly

Simple Interface Core Animation Sica can execute various animations sequentially or parallelly. Features Animation with duration and delay parallel /

A simple animated progress bar in Swift
A simple animated progress bar in Swift

DSGradientProgressView Introduction DSGradientProgressView is a simple and customizable animated progress bar written in Swift. Inspired by GradientPr

Fully customizable circular progress bar written in Swift.
Fully customizable circular progress bar written in Swift.

Fully customizable, circular progress bar written in Swift. Example To run the example project, clone the repo, and run pod install from the Example d

RAProgressRing is the simplest approach to bringing circular progress in your application with minimal code.
RAProgressRing is the simplest approach to bringing circular progress in your application with minimal code.

RAProgressRing RAProgressRing is the simplest approach to bringing circular progress in your application with minimal code. Features It's customisable

Design and prototype customized UI, interaction, navigation, transition and animation for App Store ready Apps in Interface Builder with IBAnimatable.
Design and prototype customized UI, interaction, navigation, transition and animation for App Store ready Apps in Interface Builder with IBAnimatable.

Design and prototype customized UI, interaction, navigation, transition and animation for App Store ready Apps in Interface Builder with IBAnimatable.

Letters animation allows you to click on different letters and accordingly it will animate letters in a cool way. It has a very attractive UI and is very easy to use.
Letters animation allows you to click on different letters and accordingly it will animate letters in a cool way. It has a very attractive UI and is very easy to use.

Letters Animation Cool Letters Animation in iOS written in Swift. Preview Table of content :- Description How to add in your project Requirement Licen

Numbers animation allows you to click on different numbers and accordingly it will animate numbers in a cool way. It has a very attractive UI and is very easy to use.
Numbers animation allows you to click on different numbers and accordingly it will animate numbers in a cool way. It has a very attractive UI and is very easy to use.

Numbers Animation Cool Numbers Animation in iOS written in Swift. Preview Table of content :- Description How to add in your project Requirement Licen

Reading animation allows you to click on the different page numbers and accordingly it will animate page changes in a cool way. It has a very attractive UI and is very easy to use.
Reading animation allows you to click on the different page numbers and accordingly it will animate page changes in a cool way. It has a very attractive UI and is very easy to use.

Reading Animation Cool Reading Animation in iOS written in Swift. Preview Table of content :- Description How to add in your project Requirement Licen

Owner
Gordan Glavaš
Gordan Glavaš
Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers

Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers. It's magic. Features Animate a

Robert-Hein Hooijmans 1.3k Nov 17, 2022
Chain multiple UIView animations without endlessly nesting in completion closures.

⛓ Chain multiple UIView animations without endlessly nesting in completion closures. Used in some of the more superfluous animations in the OK Video app.

Pim 78 Dec 26, 2022
Circular progress Animation

JDCircularProgress JDCircularProgress is animateable UIView that can significantly enhance your users’ experiences and set your app apart from the res

Jawad Ali 8 May 16, 2022
🚀 It Makes easy to track your task 🔥 Beautiful & Animated UI👨🏻‍💻 . Contributions are always welcome 🤗

Taskey ?? What is Taskey ?? ? Taskey is an application build in SwiftUI to track your task with a beautiful animations and UI . Also used core data to

Mohd Yasir 36 Nov 20, 2022
🚀 It Makes easy to track your task 🔥 Beautiful & Animated UI👨🏻‍💻 . Contributions are always welcome 🤗

Taskey ?? What is Taskey ?? ? Taskey is an application build in SwiftUI to track your task with a beautiful animations and UI . Also used core data to

AppLobby 36 Nov 20, 2022
Vitals - A tiny macOS process monitor lives in the menu bar, keeping track of resource usage

Vitals lives in the menu bar, keeping track of resource usage in the background so you can summon it instantly at any time.

Harry Marr 392 Dec 17, 2022
SwiftUI-Text-Animation-Library - Text animation library for SwiftUI

⚠️ This repository is under construction. SwiftUI Text Animation Library Make yo

null 28 Jan 8, 2023
An experiment for using SwiftUI's custom timing Animation to create an orbital-like animation.

Orbital-SwiftUI-Animation An experiment for using SwiftUI's custom timing curve to create an orbital-like animation. How it looks: How it works: Apply

Mostafa Abdellateef 7 Jan 2, 2023
Simple and powerful animated progress bar with dots

Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements iOS 8.0+ Swift 3.0+ Installatio

Nikola Corlija 42 Dec 5, 2022