I have a view with dynamic content.
I want to add an animation (fadeOut/fadeIn) to switch to the next view
For this purpose I tried:
.transition(.opacity)
.animation(.easeInOut, value: tabSelection)
But doesn’t work, I get a slide animation from top to bottom
My current code:
struct LongOnboardingContainerView: View {
@State var tabSelection = 0
var body: some View {
ZStack {
Step.allCases[tabSelection].view {
tabSelection += 1
}
.transition(.opacity)
.animation(.easeInOut, value: tabSelection)
}
.ignoresSafeArea(.all)
}
enum Step: Int, CaseIterable {
case parentsGoal, nickname, age, mathSkills, languageSkill
func view(pushNext: @escaping () -> ()) -> AnyView {
switch self {
case parentsGoal mathSkills, languageSkill:
return AnyView(MultiAnswerView(viewModel: .init(step: self), pushNext: pushNext))
case .nickname:
return AnyView(NickNameController.swiftUIRepresentation { vc in
vc.nextHandler = pushNext
})
default:
return AnyView(Text("Test").onTapGesture {
pushNext()
})
}
}
}
}
2
Answers
I found a workaround that simule crossDissolve animation:
I did an abstraction of your code and it shows that the transition does work with same underlying view types (Text), but does not with different ones (as for nickname and default).
So I guess the animation doesn’t like to work together with AnyView type erasure.