如何为不同视图之间的幻灯片过渡设置动画?
在下面的示例代码中,我创建了一个 Picker 来选择要显示的视图,我的目标是获得一个看起来类似于 NavigationLink 转换的转换。目前它根本没有动画。如果我将.animation(.easeInOut(duration: 2))
修改器添加到ZStack
它,它会使淡入淡出动画动画 2 秒,但我不明白为什么。
struct ContentView: View {
enum WhichScreen: String, CaseIterable {
case red, blue, green, yellow
}
@State private var whichScreen = WhichScreen.red
var body: some View {
VStack {
Picker("screen", selection: $whichScreen.animation()) {
ForEach(WhichScreen.allCases, id: \.self) { value in
Text(value.rawValue).tag(value)
}
}
.pickerStyle(SegmentedPickerStyle())
ZStack {
Color.black
switch whichScreen {
case .red:
Color.red
case .blue:
Color.blue
case .green:
Color.green
case .yellow:
Color.yellow
}
}
.transition(.slide)
}
}
}