I have a SwiftUI Picker
and I want to update another view based on its value as the user scrolls through the list. By passing a @Binding
to the picker I can update when the user stops on a selected item... but I haven't been able to find an event that fires as the list scrolls.
With the old UIPickerView
you can use titleForRow
to hook into this as mentioned in this answer. I'm looking for something similar that works with SwiftUI's Picker
.
Here's an extremely simple SwiftUI snippet that demonstrates the problem with the default behavior that I'm trying to find a way to work around:
import SwiftUI
struct ContentView: View {
@State var selected:Int = 0
var toDisplay:String {
get {
return "Selected: \(selected)"
}
}
var body: some View {
return VStack {
Text(toDisplay)
Form {
Section{
Picker(selection: $selected, label: Text("")) {
ForEach(0..<100) {
Text("Item \($0)").tag($0)
}
}
.pickerStyle(WheelPickerStyle())
.frame(width: 350, height: 250, alignment: .center)
}
}
}
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
If you run this, you can see that the Selected: #
text at the top doesn't update until the picker completely stops: