2

I have several variables that have specific relationships with each other (ie each can be converted to each other via math functions). They are all adjustable by the user and I'd like them all to change any time one of them is updated. For example:

struct SliderView: View {
    @State var feet: Float = 0
    @State var yards: Float = 0
    @State var miles: Float = 0
    var body: some View {
        VStack {
            Text("Feet = \(feet)")
            Slider(value: $feet, in: 0...5280, step: 1)
            Text("Yards = \(yards)")
            Slider(value: $yards, in: 0...1760, step: 0.1)
            Text("Miles = \(miles)")
            Slider(value: $miles, in: 0...1, step: 0.0001)
        }
    }
}

How can I make it such that changing one variable will automatically change the others?

2 Answers 2

2

I agreee with Kyle Beard.. here is an example:

class Model: ObservableObject {

    var changing = false

    @Published var feet: Float = 0 {
        willSet {
            guard !changing else {
                return
            }
            self.changing.toggle()
            self.miles = newValue / 5280
            self.yards = newValue / 3
            self.changing.toggle()
        }
    }

    @Published var yards: Float = 0 {
        willSet {
            guard !changing else {
                return
            }
            self.changing.toggle()
            self.miles = newValue / 1760
            self.feet = newValue * 3
            self.changing.toggle()
        }
    }

    @Published var miles: Float = 0 {
        willSet {
            guard !changing else {
                return
            }
            self.changing.toggle()
            self.feet = newValue * 5280
            self.yards = newValue * 1760
            self.changing.toggle()
        }
    }
}


struct ContentView: View {

    @ObservedObject var model = Model()

    var body: some View {
        VStack {
            Text("Feet = \(self.model.feet)")
            Slider(value: self.$model.feet, in: 0...5280, step: 1)
            Text("Yards = \(self.model.yards)")
            Slider(value: self.$model.yards, in: 0...1760, step: 0.1)
            Text("Miles = \(self.model.miles)")
            Slider(value: self.$model.miles, in: 0...1, step: 0.0001)
        }
    }
}

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

You could bind each value to its respective slider and then when willSet() is called, you could have a shared function that is called to then update all three based on when one was changed.

Take a look here at this answer with using Combine to track continuous updates on the slider. Note that this answer uses BindableObject, but Apple has since changed the name to ObservableObject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.