I have a simple Picker where the options available change by the view state. I would like to have the transition animated but the default animation is not good so I tried setting a .transition() and or an .animation() inside an item on the picker but it is ignored. The same happens if the transition is set on the picker itself since it's always present.
Am I doing it right and is just not posible or is there something else to do?
Code to reproduce the issue:
struct ContentView: View {
@State var list: [String] = [
"Item 4",
"Item 5",
"Item 6",
"Item 7",
"Item 8",
]
@State var selected: String?
@State var toggle: Bool = false
var body: some View {
VStack {
Picker("List", selection: $selected) {
ForEach(list, id: \.self) {
Text($0).tag($0)
// .transition(.opacity)
}
}
.pickerStyle(.segmented)
// .transition(.opacity)
HStack {
Button(action: swapOptions) {
Text("Swap")
}
}
}
.padding()
}
}
extension ContentView {
func swapOptions() {
withAnimation {
toggle.toggle()
switch toggle {
case true:
list = [
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5",
]
case false:
list = [
"Item 4",
"Item 5",
"Item 6",
"Item 7",
"Item 8",
]
}
}
}
}
``
Topic:
UI Frameworks
SubTopic:
SwiftUI