Search code examples
iosswiftswiftuiswift5xcode11

Is there a way to execute a function after an animation in SwiftUI?


In UIKit you can do something like this:

UIView.animate(withDuration: TimeInterval, animations: {
    //animation
  }) { (Bool) in
    //code which will be executed after the animation
}

Is there anything like that in SwiftUI, or can you think of a replacement?


Solution

  • There is an approach to this described here:

    https://www.avanderlee.com/swiftui/withanimation-completion-callback/

    The approach requires the use of a custom implementation of the AnimatableModifier protocol. It's not completely trivial to implement, but it does seem to solve the problem.