spacenation / swiftui-sliders

:rocket: SwiftUI Sliders with custom styles
MIT License
793 stars 84 forks source link

steps should be discrete offsets from lowerBound #71

Open shuang886 opened 10 months ago

shuang886 commented 10 months ago

Given:

    Slider(value: $v1, in: 1...10, step: 2, onEditingChanged: { _ in })
    Text("Slider: \(v1)")

    ValueSlider(value: $v2, in: 1...10, step: 2, onEditingChanged: { _ in })
        .frame(height: 30)
    Text("ValueSlider: \(v2)")
Screenshot 2023-09-18 at 9 58 09 AM

Apple's Slider() reports the discrete values [ 1, 3, 5, 7, 9 ] while ValueSlider() reports [ 1, 2, 4, 6, 8, 10 ]. IOW, Slider()'s steps are offsets from the lowerBound while ValueSlider()'s are 0-based.

This PR changes the behavior of step to match Slider().

Note: even with this fix, ValueSlider() will report [ 1, 3, 5, 7, 9, 10 ] even though the upperBound value is not a multiple of step. That's easily worked around by client code if desired, so I'm not sure it should be fixed.