SwiftUI 表自定义滑动? [英] SwiftUI Table Custom Swipe?
本文介绍了SwiftUI 表自定义滑动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法向左和向右滑动表格行?我还没有找到适用于新框架 SwiftUI 的东西,所以也许没有机会为此使用 SwiftUI?我需要删除行并使用自定义滑动
Is there a way to swipe table rows to the left and to the right? I haven't found something for the new Framework SwiftUI so maybe there is no chance to use SwiftUI for this? I need to delete rows and use custom Swipes
推荐答案
可以非常简单地实现删除操作和重新排序列表项的能力.
It is possible to implement a delete action and the ability to reorder list items quite simply.
struct SwipeActionView: View {
@State var items: [String] = ["One", "two", "three", "four"]
var body: some View {
NavigationView {
List {
ForEach(items.identified(by: \.self)) { item in
Text(item)
}
.onMove(perform: move)
.onDelete(perform: delete)
}
.navigationBarItems(trailing: EditButton())
}
}
func delete(at offsets: IndexSet) {
if let first = offsets.first {
items.remove(at: first)
}
}
func move(from source: IndexSet, to destination: Int) {
// sort the indexes low to high
let reversedSource = source.sorted()
// then loop from the back to avoid reordering problems
for index in reversedSource.reversed() {
// for each item, remove it and insert it at the destination
items.insert(items.remove(at: index), at: destination)
}
}
}
我不敢相信我以前没有找到苹果的这篇文章.编写 SwiftUI 手势.我还没有尝试过,但这篇文章似乎做得很好!
There is this article by apple that I cannot believe I didn't find previously. Composing SwiftUI Gestures. I haven't experimented with it yet, but the article seems to do a great job!
这篇关于SwiftUI 表自定义滑动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文