There are times you might want to call a function when a slider changes in SwiftUI. I did this recently to send out the slider’s value to another device when the slider is moving. I couldn’t wait to send out the final value since that would be a delayed response for the user.
After the code example, I’ll explain the three steps.
This is the variable that the slider is currently. It needs to be declared with a
State property wrapper since the value is going to change. It also needs to have a
Float type since a
Slider needs a
In this step, we are creating a Binding variable. A Binding variable is a computed property. By creating it as the value that the slider is using, we can utilize the
setter to call the function every time the value is changing.
Make sure to make the function that you need to call when the slider changes values.
or support my work monthly on Patreon.