SwiftUI Slider value

Hi Team,

I am using the Slider within a iPhone App with SwiftUI… I need it to return the following value as it slides.

Example:

.001 to .100

Here is my code below… Any suggestions to have it return the above values ?

Slider(value: $khz, in: 0…999, step: 10)
.padding(.top, -10)
.padding(.leading, 10)
.padding(.trailing, 10)

Thanks…

Craig.

Hi Craig,

You just need to declare an @State var. Like this.

  @State private var result: Double = 0
    
    var body: some View {
        
        VStack {
            
            Slider(value: $result, in: 0...999, step: 10.0)
            
            Text("\(result)")       
        }
    }

Blessings,
—Mark

Hi Mark,

Thanks for your help, however, this only gives me values as the slider moves such as 10, 20 ,30 etc.

I need it to set the state value as .001 to .100 (that’s decimal 001 through to decimal 100).

Any thoughts ?

Cheers.

Craig

Sure, you can just change the parameters: like this.

Slider(value: $result, in: 0.001...100, step: 0.001)

Blessings,
—Mark

G’day Mark.

Thanks mate… have you tried that… it’s not returning what I’m after.

Bugga hey.

Any other suggestions ?

I’ve been tying a few options… but no luck yet.

Cheers.

Craig.

Hmmm, sorry Craig, not sure what you are looking for it to return.

I relooked at you initial question and had the decimal place in the wrong place on one of the parameters. Maybe this is it?

Slider(value: $result, in: 0.001...0.100, step: 0.001)

Blessings,
—Mark

1 Like

Very close… thanks Mark,

I think I have it… Thanks for the right direction on this…

Cheers.

Craig.
Hope you have a top weekend coming up

Excellent. I am glad to hear you have it going in the direction you want now. I do appreciate how crummy it feels to be stuck!

Blessings,
—Mark

1 Like

Yep… I get stuck all the time.

Cheers.

Craig.
Old Age !

Craig,

If the extra zeros is what was throwing you off, I just learned about specifier in SwiftUI, Remove the extra zero to the result above,

Text("\(result, specifier: "%.3f")")

Blessings,
—Mark

1 Like

Hi Mark,

All good mate… I’m getting there… experimenting as I go…

Thanks for the support !!

Cheers.

Craig