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?
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.
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.
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.
.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.
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With