Given:
SectionView
as a row representation in a List
Image
as a part of the same viewRequired:
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 内置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 …