为什么删除索引后,SwiftUI会以不同的方式设置列表项的动画



我真的很难理解为什么会发生这种情况,我不知所措。

情况似乎是,动画是不同的,这取决于一个项目以前是否存在于动画项目所占据的数组的索引处。

这是我正在使用的代码:

@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: Profile.lastUsed, ascending: true)],
animation: .spring()
)
private var profiles: FetchedResults<Profile>
var body: some View {
List() {
ForEach(profiles, id: .id) { profile in
ProfileView(profile)
}
}
.listStyle(InsetListStyle())
.toolbar {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
Button(action: deleteItem) {
Label("Remove Items", systemImage: "minus")
}
}
}

private func addItem() {
let profile = Profile(context: viewContext)
profile.id = UUID()
profile.lastUsed = Date()
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error (nsError), (nsError.userInfo)")
}
}

private func deleteItem() {
if profiles.last != nil { viewContext.delete(profiles.last!) }

do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error (nsError), (nsError.userInfo)")
}
}
struct ProfileView: View {
@State var isPopover = false
var profile: Profile;

init(_ profile: Profile) {
self.profile = profile;
}

var body: some View {
Button(action: { self.isPopover.toggle() }) {
ZStack(alignment: .leading) {
RoundedRectangle(cornerRadius: 5, style: .continuous)
.fill(Color(NSColor.separatorColor).opacity(0.75))
VStack(alignment: .leading) {
Text("(profile.id!)").font(.headline)
Text("Profile last used (profile.lastUsed!, formatter: itemFormatter)").font(.body)
}.padding(12)
}
}
.buttonStyle(PlainButtonStyle())
.padding(.bottom, 8)
.popover(isPresented: self.$isPopover, arrowEdge: .trailing) {
PopoverView()
}
}
}

由此产生的效果可以在这里看到

我不确定我是否遗漏了一些信息,或者这是一个错误,因为这是在Big Sur Beta 4上。

尝试以下操作。。。

var body: some View {
Button(action: { self.isPopover.toggle() }) {
ZStack(alignment: .leading) {
RoundedRectangle(cornerRadius: 5, style: .continuous)
.fill(Color(NSColor.separatorColor).opacity(0.75))
VStack(alignment: .leading) {
Text("(profile.id!)").font(.headline)
Text("Profile last used (profile.lastUsed!, formatter: itemFormatter)").font(.body)
}.padding(12)
}
}
.buttonStyle(PlainButtonStyle())
.padding(.bottom, 8)
.animation(nil)          // << here !!
...

最新更新