有时候我们需要监听Slider值的改变,来做些事情,可以用下面的方法实现。
@State var progress: Float = 0.5 var body: some View { VStack{ Slider(value: Binding(get: { self.progress }, set: { (newVal) in self.progress = newVal self.sliderChanged() })) .padding(.all) Text(String(progress)) } } func sliderChanged() { print("Slider value changed to \(progress)") }