Home > Software design >  How to set correct maximum value for Slider in Swift?
How to set correct maximum value for Slider in Swift?

Time:06-04

I have the following code and I am wondering why the maximum for the first slider is 2.9 and the maximum for the second one is 2.0. Shouldn't be the maximum of the first slider be at 3.0? I don't see what I did different.

struct SettingsEx1View: View {
    
    @State var playSoundeffects = true
    @State var equalNotes = false
    @State var noteLength = 1.0
    @State var pauseLength = 0.5
    
    var body: some View {
        Form {
            Section(header: Text("General")) {
                Toggle("play soundeffects", isOn: $playSoundeffects)
            }
            Section(header: Text("Exercise1")) {
                VStack {
                    Text("length of notes: \(noteLength)")
                    Slider(value: $noteLength, in: 0.1...3.0, step: 0.1)
                }
                VStack {
                    Text("length of pause: \(pauseLength)")
                    Slider(value: $pauseLength, in: 0.0...2.0, step: 0.1)
                }
                Toggle("Notes can be the same", isOn: $equalNotes)
                Text("tone difference")
                Text("waveform")
            }
        }
    }
}

This is how it looks like. Is there anything that I am missing?

Thanks a lot for any help!

CodePudding user response:

Strange, seems to be a rounding issue – or a bug ?? This would be a fix:

 Slider(value: $noteLength, in: 0.1...3.01, step: 0.1)
  • Related