Easily pause and resume SwiftUI animations

Overview

swiftui-pausable-animation

Easily pause and resume SwiftUI animations!

Preview

Installation

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

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

How to use

Add the pausableAnimation modifier to your view. It takes five parameters:

  1. binding - the property changed when animating, e.g opacity, offset, rotation angle, etc. It needs to be a @Binding since the modifier will change its value when the animation is paused or resumed.
  2. targetValue - this is the final value you're setting the animated property to. E.g, if you're fading something in by changing the opacity from 0 to 1, this value will be 1. The modifier needs to know it in order to be able to resume the animation.
  3. remainingDuration - this is a block that tells how for long does the animation last, depending on its current position. E.g, if you're changing opacity from 0 to 1 over 5 seconds, and pause the animation at 2 seconds, the current opacity will be 0.4. This block should be able to take 0.4 and say that 3 seconds more are needed to complete the animation.
  4. animation - this is a block that tells which animation to use on resume. Its parameter is the duration of the animation, derived from calling remainingDuration.
  5. isPaused - set this binding to true to pause the animation and to false to resume it.
import SwiftUIPausableAnimation

struct PausableAnimationTest: View {
  @State private var angle = 0.0
  @State private var isPaused = false
  
  private let duration: TimeInterval = 6
  private let startAngle = 0.0
  private let endAngle = 360.0
  private var remainingDuration: RemainingDurationProvider<Double> {
    { currentAngle in
      duration * (1 - (currentAngle - startAngle) / (endAngle - startAngle))
    }
  }
  private let animation: AnimationWithDurationProvider = { duration in
    .linear(duration: duration)
  }
  
  var body: some View {
    VStack {
      ZStack {
        Text("I'm slowly rotating!")
          .rotationEffect(.degrees(angle))
          .pausableAnimation(binding: $angle,
                             targetValue: endAngle,
                             remainingDuration: remainingDuration,
                             animation: animation,
                             paused: $isPaused)
      }
      .frame(height: 300)
      Button(isPaused ? "Resume" : "Pause") {
        isPaused = !isPaused
      }
    }
    .onAppear {
      angle = startAngle
      withAnimation(animation(duration)) {
        angle = endAngle
      }
    }
  }
}

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...
Swiftui Spring Animations
Swiftui Spring Animations

Swiftui Spring Animations This repository serves as your reference for SwiftUI Spring Animations. It demonstrates use cases for the various types of s

Compose SpriteKit animations quickly in a declarative SwiftUI-style

ActionBuilder Caveat developer: As this package is pre-release, the API may change significantly without notice. It has not been tested, so use it at

Easy to read and write chainable animations in Objective-C and Swift
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

⛓ Easy to Read and Write Multi-chain Animations Lib in Objective-C and Swift.
⛓ 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

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

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

A library of custom iOS View Controller Animations and Interactions.
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

Simple UIButton subclass with additional state change animations (e.g. backgroundColor) and missing features
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

Snapchat-like filters, AR lenses, and real-time facial animations.
Snapchat-like filters, AR lenses, and real-time facial animations.

react-native-deepar Snapchat-like filters, AR lenses, and real-time facial animations. React-Native wrapper for DeepAR. Table of Contents What is Deep

This repo contains swift collection of gui, games, menu, animations, music, payment, etc... for iOS, macOS, watchOS and tvOS
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

Owner
Gordan Glavaš
Gordan Glavaš
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
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
(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 concept to more easily define simple keyframe / multi-step animations in SwiftUI

?? Animate A concept to more easily define simple keyframe / multi-step animations in SwiftUI, without: Defining an @State value for each property to

Seb Jachec 3 Oct 18, 2022
Easily build advanced custom animations on iOS.

INTUAnimationEngine makes it easy to build advanced custom animations on iOS. INTUAnimationEngine provides a friendly interface to drive custom animat

Intuit 1.1k Dec 14, 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
Animations created with SwiftUI.

A repository containing a variety of animations and Animated components created in SwiftUI that you can use in your own projects.

Shubham Kr. Singh 1.5k Jan 2, 2023
LottieView - Wrapper around Lottie in SwiftUI that allows chaining animations & triggering callbacks at certain frames

LottieView is a Wrapper around Lottie in SwiftUI that allows chaining animations & triggering callbacks at certain frames

Ahmed Ramy 0 Jan 23, 2022