Creating an animated pressable button
As you probably noticed, when you use a Button
component, the effect of the pressed state is particularly plain; it is just a change of opacity of the whole button. But what if we could have a sort of scale-up when it is pressed?
In this recipe, we'll see how to implement a custom button that scales up when it is pressed, and it returns to normal size when the touch is outside its frame or the button is released.
Getting ready
For this recipe, you don't need any external resources, so creating a SwiftUI project called AnimatableButtonApp
is enough.
How to do it…
We are going to implement a button that will scale up when pressed and scale down when released. Also, we need to have a callback to be called when the button is released.
If you try to add an animation to a Button
object using DragGesture
to simulate the touch, you'll see that the action associated is not called, but we'll discuss this in the...