0

is there the possibility in some way to call a handler or closure when a certain animation on a view using the animation-View-Modifier has ended?

Like the "animate" - Feature in UIKit:

https://developer.apple.com/documentation/uikit/uiview/1622515-animate

I'm using this:

  .animation(.easeInOut(duration: animationDurationOffsetX), value: xOffset) 

But as I already said there is no way to pass a completion block.

Thank you!

Best Regards,

Frank

Frank Marx
  • 151
  • 11

1 Answers1

0

If your deployment target is at least iOS 17 (or tvOS 17, etc., to be released late 2023), you can use the addAnimationCompletion method to add a completion handler to the Transaction associated with the animation. This works for me in Xcode 15.0 beta 1 in an iOS playground:

struct Demo: View {
    @State var flag = false

    var body: some View {
        (flag ? Color.red : Color.white)
            .transaction {
                $0.addAnimationCompletion {
                    print("animated")
                }
            }
            .animation(.easeOut, value: flag)
            .frame(width: 100, height: 100)
            .padding()
            .onTapGesture {
                flag.toggle()
            }
    }
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848