0

Trying to build an app navigation view that does programmatic push/pop animations with given views. The main animation happens in containedView() where I show the new view and hide the current view with animation. The problem I have is that the animation happens instantly, so it doesn't happen at all.

I am curious how to debug this or if anyone has any recommendations on how to fix this?

enum AppNavigationState
{
    case root
    case push(currentView: AnyView, newView: AnyView)
    case pop(currentView: AnyView, newView: AnyView)
}


class AppNavigationViewModel: ObservableObject {
    public var navigationStack: [AnyView]
    @Published public var navigationState: AppNavigationState

    init(rootView: AnyView) {
        navigationStack = [rootView]
        navigationState = AppNavigationState.root
    }

}

struct AppNavigationView: View {
    @ObservedObject var appNavigationViewModel: AppNavigationViewModel

    var body: some View {
        containedView()
    }

    private func containedView() -> AnyView {
        switch(appNavigationViewModel.navigationState) {
        case .root:
            return appNavigationViewModel.navigationStack.last!
        case let .pop(currentView, newView):
            return AnyView(ZStack {
                newView
                currentView
                    .transition(AnyTransition.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
                    .animation(.easeOut)
            })
        case let .push(currentView, newView):
            return AnyView(ZStack {
                currentView
                newView
                    .transition(AnyTransition.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
                    .animation(.easeOut)
            })
        }
    }

    public func pushView(view:AnyView) {
        if let currentView = appNavigationViewModel.navigationStack.last {
            appNavigationViewModel.navigationStack.append(view)
            appNavigationViewModel.navigationState = .push(currentView: currentView, newView: view)
        }
    }

    public func popView() {
        if (appNavigationViewModel.navigationStack.count > 1) {
            let currentView = appNavigationViewModel.navigationStack.removeLast()
            let newView = appNavigationViewModel.navigationStack.last!
            appNavigationViewModel.navigationState = .push(currentView: currentView, newView: newView)
        }
    }
}

enter image description here

Zorayr
  • 23,770
  • 8
  • 136
  • 129
  • 2
    See for solution in [How do I add Animations to Transitons between custom NavigationItems made from AnyView?](https://stackoverflow.com/a/59087574/12299030) – Asperi May 08 '20 at 18:08
  • I commented on your original post, but the code there doesn't seem to be working anymore. You end up seeing Page 1, Page 2, and a slide in animation of Page 2 – Zorayr May 08 '20 at 19:36

0 Answers0