Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 870
Paul Avatar asked Jul 21 '19 14:07

Paul


People also ask

How do I know if animation is complete SwiftUI?

While animating, SwiftUI changes the old input value to the new target value using this property. This value is set to the old value until the animation completes. In other words, once the animation is done it will match our expected outcome. This is exactly how we know that an animation completes.

How do I use animation in SwiftUI?

SwiftUI has built-in support for animations with its animation() modifier. To use this modifier, place it after any other modifiers for your views, tell it what kind of animation you want, and also make sure you attach it to a particular value so the animation triggers only when that specific value changes.

Does SwiftUI use Core Animation?

SwiftUI uses Core Animation for rendering by default, and its performance is great for most animations. But if you find yourself creating a very complex animation that seems to suffer from lower framerates, you may want to utilize the power of Metal, which is Apple's framework used for working directly with the GPU.

How do I animate a view in SwiftUI?

.animation is a modifier that stacks on to a SwiftUI View like any other. If a view has multiple changing attributes, a single Animation can apply to all of them. This adds a little rotation to the moon button so the crescent lines up sideways when the moon view appears. These will animate together since you add the animation at the end.

Why use the simulator instead of SwiftUI preview?

An advantage of building and running in the simulator instead of the SwiftUI preview window is that you can enable the Debug ▸ Slow Animations flag. This will drastically slow down any animation so you can see the subtle differences more clearly.

How to make a button pulse forever in SwiftUI?

Change the animation to: The button will now pulse forever. You can also use repeatCount (autoreverses:) to repeat the animation a limited number of times. .animation is a modifier that stacks on to a SwiftUI View like any other. If a view has multiple changing attributes, a single Animation can apply to all of them.

How to call a function from inside the Swift UI structure?

There is a reason, when writing in swift UI (iOS Apps), that there is a View protocol that needs to be followed. Calling functions from inside the structure is not compliant with the protocol. The best thing you can do is the .on (insertTimeORAction here).


1 Answers

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.

like image 132
B.T. Avatar answered Oct 20 '22 21:10

B.T.