我有一个显示 CoreData FetchRequest 的列表,并且我有一个 Picker 可以更改列表的排序方式。我实现这个的当前方式看起来像:
struct ParentView: View {
enum SortMethod: String, CaseIterable, Identifiable {
var id: Self { self }
case byName = "Name"
case byDateAdded = "Date Added"
}
@State private var currentSortMethod = SortMethod.byName
var body: some View {
ItemListView(sortMethod: currentSortMethod) // See child view implementation below
.toolbar {
ToolbarItem(placement: .principal) {
Picker("Sort by", selection: $currentSortMethod) {
ForEach(SortMethod.allCases) { sortMethod in
Text(sortMethod.rawValue)
}
}
}
}
}
}
subview 看起来像:struct ItemListView: View {
@Environment(\.managedObjectContext) private var managedObjectContext
@FetchRequest var items: FetchedResults<Item>
init(sortMethod: ParentView.SortMethod) {
let sortDescriptor: NSSortDescriptor
switch sortMethod {
case .byName:
sortDescriptor = NSSortDescriptor(keyPath: \Item.name, ascending: true)
case .byDateAdded:
sortDescriptor = NSSortDescriptor(keyPath: \Item.dateAdded, ascending: true)
}
_items = .init(
entity: Item.entity(),
sortDescriptors: [sortDescriptor],
predicate: nil,
animation: .default
)
}
var body: some View {
List {
ForEach(items) { item in
SingleItemView(item)
}
}
}
}
但是,当我更改排序选项时,列表不会为重新排序设置动画(可能是由于整个 ItemListView
正在重建。如果我在父 View 中将 .animation(.default)
添加到 ItemListView()
,列表会在重新排序时进行动画处理,但从其他 View 返回时也有奇怪的动画。我似乎无法弄清楚我可以在哪里添加 withAnimation { }
block 。或者是否有更好的方法来解决这个问题,它对 SwiftUI 来说更自然,因此允许一些默认动画?
最佳答案
绑定(bind)可以附加动画,因此请尝试以下操作(或使用您希望的任何动画参数)
Picker("Sort by", selection: $currentSortMethod.animation()) // << here !!
关于swift - 如何使用 SwiftUI 和 CoreData (@FetchRequest) 对动态列表排序进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63946739/