gpt4 book ai didi

ios - 在 SwiftUI 中删除 LazyVStack 和 ForEach 中的行

转载 作者:行者123 更新时间:2023-12-04 11:26:43 27 4
gpt4 key购买 nike

List可以简单地使用 .onDelete修饰符从列表中删除行。但是我们怎样才能在 ForEach 中做同样的事情?显示在 LazyVStack 中.我正在使用 SwipeCell应用拖动手势并显示删除按钮,但我实际上如何从 CoreData 中删除,特别是如何访问 IndexSet在这种情况下

LazyVStack {
ForEach(items) { item in
Text("\(item.name)")
.swipeCell(cellPosition: .right, leftSlot: nil, rightSlot: slot1)
.alert(isPresented: $showAlert){
Alert(title: Text("Delete Task?"), message: nil, primaryButton:.destructive(Text("Delete"), action: {


// HOW DO I DELETE HERE ?


dismissDestructiveDelayButton()
}),secondaryButton: .cancel({dismissDestructiveDelayButton()}))
}
}
}

private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { items[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}

最佳答案

我制作了一个 ViewModifier,它允许你删除任何你想要的 View ,类似于 Apple 动画。
使用时只需调用.onDelete { //perform deletion }在任何 View 上。

struct Delete: ViewModifier {

let action: () -> Void

@State var offset: CGSize = .zero
@State var initialOffset: CGSize = .zero
@State var contentWidth: CGFloat = 0.0
@State var willDeleteIfReleased = false

func body(content: Content) -> some View {
content
.background(
GeometryReader { geometry in
ZStack {
Rectangle()
.foregroundColor(.red)
Image(systemName: "trash")
.foregroundColor(.white)
.font(.title2.bold())
.layoutPriority(-1)
}.frame(width: -offset.width)
.offset(x: geometry.size.width)
.onAppear {
contentWidth = geometry.size.width
}
.gesture(
TapGesture()
.onEnded {
delete()
}
)
}
)
.offset(x: offset.width, y: 0)
.gesture (
DragGesture()
.onChanged { gesture in
if gesture.translation.width + initialOffset.width <= 0 {
self.offset.width = gesture.translation.width + initialOffset.width
}
if self.offset.width < -deletionDistance && !willDeleteIfReleased {
hapticFeedback()
willDeleteIfReleased.toggle()
} else if offset.width > -deletionDistance && willDeleteIfReleased {
hapticFeedback()
willDeleteIfReleased.toggle()
}
}
.onEnded { _ in
if offset.width < -deletionDistance {
delete()
} else if offset.width < -halfDeletionDistance {
offset.width = -tappableDeletionWidth
initialOffset.width = -tappableDeletionWidth
} else {
offset = .zero
initialOffset = .zero
}
}
)
.animation(.interactiveSpring())
}

private func delete() {
offset.width = -contentWidth
action()
}

private func hapticFeedback() {
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
}

//MARK: Constants

let deletionDistance = CGFloat(200)
let halfDeletionDistance = CGFloat(50)
let tappableDeletionWidth = CGFloat(100)


}

extension View {

func onDelete(perform action: @escaping () -> Void) -> some View {
self.modifier(Delete(action: action))
}

}

关于ios - 在 SwiftUI 中删除 LazyVStack 和 ForEach 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64103113/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com