怎么我可以移动列表中的项目没有拨EditMode

0

的问题

我目前正在建立一个待办列表中的应用程序在SwiftUI. 一个特征,我真的想要实现的能力进行排序列出手,所以我已经整合的功能使用 .onMove 修改我的 ForEach 环填充我的 List但是我仍然不得不切换EditMode手,所以我设置的EditMode的名单 .active 如下:

import SwiftUI

struct ContentView: View {
@State private var items = ["1", "2", "3"]
@State var editMode: EditMode = .active

var body: some View {
    List {
        ForEach(items, id: \.self) { item in
            Text("Item \(item)")
        }
        .onMove(perform: { _, _  in })
    }
    .environment(\.editMode, $editMode)
}
}

但是我不高兴与这一执行,因为我仍然必须使用握从EditMode,而且它也符SwipeActions以及按功能。

所以我怎么可以移动列表中的项目而不使用EditMode?

swiftui swiftui-list
2021-11-22 16:50:54
1

最好的答案

0

根据Asperi的回答 个问题我实现的拖拉手势来解决这一问题如下:

struct ContentView: View {

@State var items = [Item(id: 1), Item(id: 2), Item(id: 3), Item(id: 4)]
@State private var dragging: Item?

var body: some View{
    List{
        ForEach(items){ item in
            Text("Item \(item.id)")
                .onDrag {
                    self.dragging = item
                    return NSItemProvider(object: NSString())
                }
                .onDrop(of: [UTType.text], delegate: DragDelegate(current: $dragging))
        }
        .onMove(perform: {_, _  in })
    }
}
}

使用 DropDelegate 执行:

struct DragDelegate<Item: Equatable>: DropDelegate {
@Binding var current: Item?

func dropUpdated(info: DropInfo) -> DropProposal? {
    DropProposal(operation: .move)
}

func performDrop(info: DropInfo) -> Bool {
    current = nil
    return true
}
}

注:该项目现在必须符合 Identifiable & Equatable 所以最小限度的实现是:

struct Item: Identifiable, Equatable{
let id: Int
}

而且你还需要进口:

import UniformTypeIdentifiers

为了使用拖功能

2021-11-24 13:26:10

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................