SwiftUI: How to get continuous updates from Slider

后端 未结 6 1761
悲哀的现实
悲哀的现实 2020-12-11 01:56

I\'m experimenting with SwiftUI and the Slider control like this:

struct MyView: View {

    @State private var value = 0.5

    var body: some View {
               


        
6条回答
  •  盖世英雄少女心
    2020-12-11 02:43

    In Version 11.4.1 (11E503a) & Swift 5. I didn't reproduce it. By using Combine, I could get continuously update from slider changes.

    class SliderData: ObservableObject {
    
      @Published var sliderValue: Double = 0
      ...
    
    }
    
    struct ContentView: View {
    
      @ObservedObject var slider = SliderData()
    
      var body: some View {
        VStack {
          Slider(value: $slider.sliderValue)
          Text(String(slider.sliderValue))
        } 
      }
    }  
    
    

提交回复
热议问题