动画在swiftui的工作表内不工作

yacmzcpb  于 2023-01-19  发布在  Swift
关注(0)|答案(1)|浏览(120)

我正在使用一个工作表来呈现一个选项列表,并在单击该选项时,我想用从拖尾滑动的动画来更改视图。根据我的理解和我在各个网站上读到的内容,我已经编写了此代码,但我不确定为什么它不能按预期的方式工作。我只想知道此代码到底在哪里出错。

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)
    }
}

我尝试创建一个视图,并使用带有bool变量的withanimation尝试更改视图。

drnojrws

drnojrws1#

听起来你想要的是把checkedList推到一个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()

                    }
                }
            })
    }
}

相关问题