向上移动列表项会导致动画损坏

时间:2020-04-02 18:23:50

标签: swiftui swiftui-list

我只是尝试使用一些非常基本的代码在列表中移动项目,并且一切正常,除非我尝试将项目移动到其上方一个地方。

似乎首先交换了这两行的数据,然后启动动画将行从原始位置移出。

是否有人知道会导致什么的原因,更重要的是,如何解决?谢谢!

struct ContentView : View {

    @State var zahlen = [1,2,3,4,5]

    var body: some View {
        List {
            ForEach(zahlen, id: \.self) { z in
                Text("\(z)")
            }.onMove(perform: self.move)
        }.environment(\.editMode, .constant(.active))
    }

    func move(source: IndexSet, destination: Int) {
        zahlen.move(fromOffsets: source, toOffset: destination)
    }
}

2 个答案:

答案 0 :(得分:0)

对我来说这是一个快捷的错误。

 struct ContentView : View {

@State var zahlen = [1,2,3,4,5]

var body: some View {
    List {
        ForEach(zahlen, id: \.self.hashValue) //<<-- here it is "hashValue is deprecated as a Hashable requirement." but until the bug is fixed i think we can use it. 
                                             { z in
            Text("\(z)")
        }.onMove(perform: self.move)
    }.environment(\.editMode, .constant(.active))
}

func move(source: IndexSet, destination: Int) {
    zahlen.move(fromOffsets: source, toOffset: destination)
}

}

答案 1 :(得分:0)

对于使用iOS 14.2的我来说,此错误似乎不再发生。