I am trying to build a navigation drawer with slide animation for left menu content and opacity animation for the background of the menu.
The code below works fine for me except for the animation. I am not sure where exactly the animation went wrong and it's not working.
Here is my code.
struct LeftNavigationView:View {
@EnvironmentObject var viewModel:ViewModel
var body: some View {
ZStack {
Color.black.opacity(0.8)
.ignoresSafeArea()
.transition(.opacity)
.animation(.default)
VStack {
Button(action: {
self.viewModel.isLeftMenuVisible.toggle()
}, label: {
Text("Close Me")
})
}
.frame(maxWidth:.infinity, maxHeight: .infinity)
.background(Color.white)
.cornerRadius(10)
.padding(.trailing)
.padding(.trailing)
.padding(.trailing)
.padding(.trailing)
.transition(
.asymmetric(
insertion: .move(edge: .leading),
removal: .move(edge: .leading)
)
)
.animation(.default)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.animation(.default)
}
}
class ViewModel: ObservableObject {
@Published var isLeftMenuVisible:Bool = false
}
struct ContentView: View {
@StateObject var viewModel:ViewModel = ViewModel()
var body: some View {
ZStack {
NavigationView {
VStack(alignment:.leading) {
Button(action: {
self.viewModel.isLeftMenuVisible.toggle()
}, label: {
Text("Button")
})
}.padding(.horizontal)
.navigationTitle("ContentView")
}
if self.viewModel.isLeftMenuVisible {
LeftNavigationView()
}
}.environmentObject(self.viewModel)
}
}
Any help will be appreciated.