提问人:Brenton Beltrami 提问时间:10/15/2020 更新时间:10/15/2020 访问量:289
Swiftui:检测 onMove Start
Swiftui: Detecting onMove Start
问:
目标:我希望能够在 editMode 中调整步进器,但是当用户开始移动标题行时,要么拖动步进器,要么隐藏所有步进器行,直到移动完成。
当前:当用户移动标题行时,它会在所有行之间移动,而不仅仅是标题行。
试:我查看了onMoved文档,似乎只有在移动完成后才能做一些事情。我还尝试使用长按和点击手势,但它们似乎只适用于文本本身,而不是行。我不确定这目前是否可行。
法典:
struct ContentView: View {
@ObservedObject var items = ItemGroup()
@State private var editMode = EditMode.active
var body: some View {
NavigationView{
List {
ForEach(Array(items.group.enumerated()), id: \.element.id) { index, item in
Text(items.group[index].item)
.deleteDisabled(!editMode.isEditing)
if editMode.isEditing {
let stepper = items.group[index].stepper
Stepper("Stepper: \(stepper)", value: $items.group[index].stepper, in: 1...10)
.moveDisabled(true)
.deleteDisabled(true)
}
}
.onMove(perform: onMove)
}
.navigationBarTitle("List")
.navigationBarItems(leading: EditButton(), trailing: addButton)
.environment(\.editMode, $editMode)
}
}
private var addButton: some View {
switch editMode {
case .active:
return AnyView(Button(action: onAdd) { Image(systemName: "plus") })
default:
return AnyView(EmptyView())
}
}
func onAdd() {
let newItem = SingleItem(item: "Word - \(items.group.count)", supplement: [""], stepper: 1, current: 1)
self.items.group.append(newItem)
}
private func onMove(source: IndexSet, destination: Int) {
items.group.move(fromOffsets: source, toOffset: destination)
}
}
struct SingleItem: Identifiable {
let id = UUID()
var item: String
var supplement: [String]
var stepper: Int
var current: Int
}
class ItemGroup: ObservableObject{
@Published var group = [SingleItem]()
}
答: 暂无答案
评论
Text(items.group[index].item)
Text(items.group[index].item)