简体   繁体   English

在 SwiftUI 中初始化 Slider 值

[英]Initializing a Slider value in SwiftUI

In the process of learning SwiftUI , I am having this issue.在学习SwiftUI的过程中,我遇到了这个问题。

I have this view with a slider:我对 slider 有这样的看法:

struct RangeSpanView: View {
    var minimumValue,maximumValue:Double

    init(minSlide: UInt64,
         maxSlide: UInt64) {
        self.minimumValue = Double(minSlide)
        self.maximumValue = Double(maxSlide)
    }

    @State var sliderValue = 0.0

    var body: some View {
        VStack {
            HStack {
                Text("\(Int(minimumValue))")
                Slider(value: $sliderValue,
                       in: minimumValue...maximumValue)
                Text("\(Int(maximumValue))")
            }.padding()
            Text("\(Int(sliderValue))")
            Spacer()
        }
    }
}

And this is the code where the view is loaded.这是加载视图的代码。

var body: some View {
    NavigationLink(destination: RangeSpanView(minSlide: myMinVal,
                                              maxSlide: myMaxVal),
                   label: {
                       .......... // Useful code.
    })
}

It works fine using the slider.使用 slider 可以正常工作。 There is only a little inconsistency detail concerning the initialization of the sliderValue that I would like to fix.关于sliderValue的初始化,只有一点不一致的细节我想修复。

The way it is in the current code is that sliderValue is initialized to 0.0, which is not exactly what I like when the min and max values are respectively 150.0 and 967.0 (for example).当前代码中的方式是将sliderValue初始化为0.0,当最小值和最大值分别为150.0和967.0(例如)时,这并不是我喜欢的。

I have tried various solutions to initialize sliderValue to minimumValue (for example) to make it consistent.我尝试了各种解决方案来将sliderValue初始化为minimumValue (例如)以使其保持一致。 But it always fails with some error message complaining about some "expected argument type 'Binding" in the Slider object.但它总是失败并出现一些错误消息,抱怨Slider object 中的一些“预期参数类型'绑定”。

So, is there a proper way to solve this problem?那么,有没有合适的方法来解决这个问题呢?

How about something like this:像这样的东西怎么样:

struct TestView: View {
    let myMinVal: UInt64 = UInt64.random(in: 100...200)
    let myMaxVal: UInt64 = UInt64.random(in: 400...600)
    
    @State var sliderValue: Double = 0
    
    var body: some View {
        RangeSpanView(
            minimumValue: Double(myMinVal),
            maximumValue: Double(myMaxVal),
            sliderValue: $sliderValue
        ).onAppear {
            self.sliderValue = Double(self.myMinVal)
        }
    }
}

struct RangeSpanView: View {
    var minimumValue: Double
    var maximumValue: Double

    @Binding var sliderValue: Double

    var body: some View {
        VStack {
            HStack {
                Text("\(Int(minimumValue))")
                Slider(value: $sliderValue,
                       in: minimumValue...maximumValue)
                Text("\(Int(maximumValue))")
            }.padding()
            Text("\(Int(sliderValue))")
            Spacer()
        }
    }
}

You can use a @Binding var on the SliderValue so that you can set starting value before you present it.您可以在 SliderValue 上使用 @Binding var,以便在呈现它之前设置起始值。 I used onAppear but you can set the sliderValue on wherever you are setting the myMinVal我使用了 onAppear 但您可以在设置 myMinVal 的任何位置设置 sliderValue

You can initialize your @State variable with the lowest min value of the slider.您可以使用 slider 的最低最小值初始化您的 @State 变量。 So you won't need to do this in the willApear() method.因此,您不需要在 willApear() 方法中执行此操作。

struct RangeSpanView: View {
    var minimumValue,maximumValue:Double

    //declare State here
    @State var sliderValue : Double

    init(minSlide: UInt64,
         maxSlide: UInt64) {
        self.minimumValue = Double(minSlide)
        self.maximumValue = Double(maxSlide)

        //Initialize the State with the lowest value
        self._sliderValue = State(initialValue: self.minimumValue)
    }

I think Ludyem is onto somthing.我认为 Ludyem 正在做某事。 On this site I found an explanation as to why setting the value in the init wont work.这个网站上,我找到了为什么在 init 中设置值不起作用的解释。

But if we use the onAppear function on the slider we can ensure the default value is within range like this但是如果我们在 slider 上使用 onAppear function 我们可以确保默认值在这样的范围内

                .onAppear {
                    if(self.sliderValue < self.minimumValue) {
                        self.sliderValue = self.minimumValue
                    }
                }

So the whole thing would look like this所以整个事情看起来像这样

struct RangeSpanView: View {
  let minimumValue,maximumValue:Double

  init(minSlide: UInt64, maxSlide: UInt64) {
    self.minimumValue = Double(minSlide)
    self.maximumValue = Double(maxSlide)
  }

@State var sliderValue = 0.0


var body: some View {
    VStack {
        Text("hi guys")
        HStack {
            Text("\(Int(minimumValue))")
            Slider(value: $sliderValue,
                in: minimumValue...maximumValue)
                .onAppear {
                    if(self.sliderValue < self.minimumValue) {
                        self.sliderValue = self.minimumValue
                    }
                }
            Text("\(Int(maximumValue))")
        }.padding()
        Text("\(Int(sliderValue))")
        Spacer()
    }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM