How to detect when dragging or editing has stopped
doodahdayz opened this issue · comments
This slider looks great and I love the ability to configure it... but I cannot find a way to only execute a function / closure when the user has stopped dragging... if you want to use the value of the slider to make a network call you surely don't want to be firing off requests as the user is dragging the slider around.. Is there anyway to know when "editing" has stopped with this slider?
Hey!
The first think comes to my mind is to use debounce from Combine.
I'll think about it for better ideas and I'll examples.
Thanks to the help of the venerable Obi Wu Kenobi I was able to take his example using the SwiftUI slider control and adapt it using the CompactSlider to make this work. The Apple slider control on the Apple Watch is really just a stepper control and it doesn't fire off like on iOS or macOS when the editing ends...
This worked... adapting Obi Wu's example and using the CompactSliderState.isDragging
The cllHome function referenced is just a function call that will make a URL session call to a remote system and is called when the slider has stopped moving. Thus not calling every change being registered as the slider is moved.
@State private var dimLevel : Float = 0
@State private var sliderState: CompactSliderState = .zero
VStack {
Text( "\(dimLevel,specifier: "%.0f") %" )
CompactSlider(value: $dimLevel, in: 0...101, step: 5, handleVisibility: .standard, state: $sliderState, valueLabel: {
}).onChange(of: sliderState.isDragging == false, perform: {newValue in
callHome(ref: ourData.devices.first!.ref, state: Int(dimLevel))
})
.compactSliderStyle(.custom)
}
Wow! That’s a great solution!
If you will need any help let me know
That’s an Obi WU Kenobi solution. Nerf Herder Not he is. The SwiftUI force is strong with him.
You should put an example like this in your documentation. Also, is there a way to have the hash marks on the slider always visible? Thank you for a very nice control.
Could you provide a link to the solution you are referring to?
Unfortunately the scale visibility is not configurable for now, but I'll make some changes in the next versions.
I'll make changes. No worries