在SwiftUI中,如何为发布视图更改设置动画?

我想为视图更改设置动画。 特别是,我完成了一个视图,其中一个子视图是一个不断变化的视图。

struct MasterView: View {

    @State var playerLeft: Bool = false
    @ObservedObject var viewModel: MasterViewModel

    var body: some View {
        ZStack {
            WaitPlayerBackView(isShowing: self.$playerLeft) {
                self.viewModel.currentView
            }
        }
    }
}

视图模型是一个ObservableObject类。 currentView由异步线程中调用的方法更改,如下所示:

class MasterViewModel: ObservableObject {
    @Published var currentView: AnyView = AnyView(EmptyView())

    func changeView() {
        self.currentView = AnyView(NightView(sceneDelegate: self.sceneDelegate, acted: false))
    }
}

就像是Android片段的副本(请比较)。 如何为该视图更改设置动画? 我尝试了不同的选择,例如:

self.viewModel.currentView.animate(.default)

要么

struct MasterView: View {

    @State var playerLeft: Bool = false
    @ObservedObject var viewModel: MasterViewModel

    var body: some View {
        ZStack {
            WaitPlayerBackView(isShowing: self.$playerLeft) {
                self.viewModel.currentView
            }
        }.animate(self.viewModel.animate ? .easeIn(duration: 1) : .none)
    }
}

class MasterViewModel: ObservableObject {
    @Published var currentView: AnyView = AnyView(EmptyView())
    @Published var animate = false

    func changeView() {
        self.animate = false
        self.currentView = AnyView(NightView(sceneDelegate: self.sceneDelegate, acted: false))
        self.animate = true
    }
}

但是,它们都不起作用。

评论