Home > front end >  Animation not working inside sheet for swiftui
Animation not working inside sheet for swiftui

Time:01-18

I am using a sheet to present a list of options and on click of the option I want to change the view with the animation of sliding from trailing. As per my understanding and what I have read on various sites I have written this code but I am not sure why it is not working the way intended. I just want to know where exactly this code went wrong.

struct XYZ: App {
    let persistenceController = PersistenceController.shared
    @State var isPresented : Bool = false
    @State var isSwiped : Bool = false
    var body: some Scene {
        WindowGroup {
            optionList(isPresented: $isPresented)
                .sheet(isPresented: $isPresented, content: {
                    Text("This is from modal view!")
                        .onTapGesture {
                            withAnimation(Animation.easeIn(duration: 10)){
                                isSwiped.toggle()
                            }
                        }
                    if isSwiped {
                        checkedList()
                            .transition(.move(edge: .trailing))
                    }
                })
        }
    }
}
struct optionList : View {
    @Binding var isPresented : Bool
    var body: some View {
        Text("This is a testView")
            .onTapGesture {
                withAnimation{
                    isPresented.toggle()
                }
            }
    }
}

struct checkedList : View {
    @State var name : String = "WatchList"
    var arr = ["First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh"]
    @State var mp : [Int:Int] = [:]
    var body: some View {
        VStack{
            HStack{
                TextField("WatchlistName", text: $name)
                    .padding(.all)
                Image(systemName: "trash.fill")
                    .padding(.all)
                    .onTapGesture {
                        print("Delete watchList!!")
                    }
            }
            ScrollView{
                ForEach(arr.indices) { item in
                    HStack (spacing: 0) {
                        Image(systemName: mp.keys.contains(item) ? "checkmark.square" : "square")
                            .padding(.horizontal)
                        Text(arr[item])
                        
                    }
                    .padding(.bottom)
                    .frame(width: UIScreen.main.bounds.width, alignment: .leading)
                    .onTapGesture {
                        if mp.keys.contains(item) {
                            mp[item] = nil
                        } else {
                            mp[item] = 1
                        }
                        
                        
                    }
                }
            }
            Button {
                print("Remove Ticked Elements!")
                deleteWatchListItem(arr: Array(mp.keys))
            } label: {
                Text("Save")
            }
        }
    }
    
    func deleteWatchList(ind: Int){
        print(ind)
    }
    
    func deleteWatchListItem(arr : [Int]) {
        print(arr)
    }
}

I tried to create a view and with the animation using withanimation with a bool variable tried to change the view.

CodePudding user response:

It sounds like what you want is to push the checkedList on to a NavigationStack

struct ContentView: View {

    @State var isPresented : Bool = false

    var body: some View {
        Text("This is a testView")
            .onTapGesture {
                    isPresented.toggle()
            }
            .sheet(isPresented: $isPresented, content: {
                NavigationStack {
                    NavigationLink("This is from modal view!") {
                        checkedList()

                    }
                }
            })
    }
}

enter image description here

  • Related