标签: swiftui-swipeactions

Programmatically show swipe actions in SwiftUI

Given:

  1. SectionView as a row representation in a List
  2. Swipe actions associated with this view
  3. Tappable Image as a part of the same view

Required:

Display swipe actions on Image tap, not only using a swipe gesture.

Will be thankful for any clues regarding implementation.

struct SectionView: View {

@ObservedObject var viewModel: SectionVM

var body: some View {
    HStack {
        ...
        Image("iconVerticalDots") 
            .renderingMode(.template)
            .foregroundColor(AppColors.accent.asColor)
            .padding([.vertical, .leading], 4.0)
            .onTapGesture {
                //Show swipe actions
            }
    }
    .swipeActions {
        actions
    }
}

var …
Run Code Online (Sandbox Code Playgroud)

swiftui swiftui-list swiftui-swipeactions

9
推荐指数
0
解决办法
550
查看次数

SwiftUI 在 iOS 15 上使用 editMode 的 onDelete 修饰符以及自定义滑动操作

初始描述

我目前有一个动态列表,我正在使用 SwiftUI 内置editMode功能和内置功能EditButton(),以便让用户从列表中删除项目。

对于删除部分,我使用的onDelete修改器添加了尾随的红色删除滑动手势,就像现在一样。

这是一个例子:

List {
    ForEach(someList) { someElement in
        Text(someElement.name)
    }
    .onDelete { (indexSet) in }
    .onMove { (source, destination) in }
}
.toolbar {
    ToolbarItem(placement: .navigationBarLeading) {
        EditButton()
    }
}
.environment(\.editMode, $editMode)
Run Code Online (Sandbox Code Playgroud)

目标

现在我还想使用 iOS 15.swipeActions修改器,它允许添加我们自己的自定义前导或尾随滑动手势。

我希望用户也能够通过向右滑动来编辑列表元素,因此我在行中添加了一个前导滑动操作:

Text(someElement.name)
.swipeActions(edge: .leading) {
    Button("Edit") { }
}
Run Code Online (Sandbox Code Playgroud)

按钮操作显然会包含允许编辑的代码。

问题

使用.swipeActions修饰符会破坏 的正常行为editMode,尤其是.onDelete修饰符。事实上,通过这种设置,我无法再向左滑动来删除该行。

所以问题是:如何在列表上使用 SwiftUIeditMode.onDelete修饰符以及我自己的自定义leading滑动操作?

最小可重复示例

Xcode …

editmode ios swiftui swiftui-swipeactions

8
推荐指数
1
解决办法
2654
查看次数