目标是具有以下行为的修改:

(但只有两个按钮-左边一个,右边一个)
行为:
最小可重现性示例:
import SwiftUI
public extension View {
func SwiperizeItem(closureL: @escaping () -> (), closureR: @escaping () -> ()) -> some View
{
self.modifier( SwiperizeItemModifier(closureL: closureL, closureR: closureR) )
}
}
struct SwiperizeItemModifier: ViewModifier {
@State var dragOffset = CGSize.zero
@State var offset1Shown = CGSize(width: 100, height: 0)
@State var offset1Click = CGSize(width: 250, height: 0)
@State var offset2Shown = CGSize(width: -100, height: 0)
@State var offset2Click = CGSize(width: -250, height: 0)
@State var BackL = Color.green
@State var BackR = Color.red
@State var ForeColorL = Color.white
@State var ForeColorR = Color.white
@State var closureL: () -> Void
@State var closureR: () -> Void
func body(content: Content) -> some View {
HStack{
Button(action: { closureL() } ) {
Text("Left")
.foregroundColor(ForeColorL)
}
.background(BackL)
.frame(maxWidth: dragOffset.width > 0 ? dragOffset.width : 0)
.fixedSize()
content
//.padding([.leading, .trailing], 20)
.animation(.spring())
.offset(x: self.dragOffset.width)
.gesture(DragGesture()
.onChanged(){
value in
self.dragOffset = value.translation
}
.onEnded(){
value in
if ( dragOffset.width > 0 ) {
if ( dragOffset.width < offset1Shown.width) {
self.dragOffset = .zero
}
else if ( dragOffset.width > offset1Shown.width && dragOffset.width < offset1Click.width ) {
self.dragOffset = offset1Shown
}
else if ( dragOffset.width > offset1Click.width ) {
self.dragOffset = .zero
closureR()
}
}
else {
if ( dragOffset.width > offset2Shown.width) {
self.dragOffset = .zero
}
else if ( dragOffset.width < offset2Shown.width && dragOffset.width > offset2Click.width ) {
self.dragOffset = offset2Shown
}
else if ( dragOffset.width < offset2Click.width ) {
self.dragOffset = .zero
closureL()
}
}
}
)
}
}
}
// ____________________
struct GuestureItem_Previews: PreviewProvider {
static var previews: some View {
Group {
Text("Hello")
.padding(.all, 30)
.background( Color( NSColor.red ) )
.SwiperizeItem(closureL: { print("click left") }, closureR: { print("click right") })
}
}
}所以..。我的问题是:

我认为这个解决方案可能与SwiftUI组件的新版本有关:
LazyHGrid或OutlineGroup。https://developer.apple.com/videos/play/wwdc2020/10031 .onDelete()不是我的解决方案,因为它不可能做两个边按钮,也不可能编辑“删除”文本
发布于 2020-07-04 13:34:21
“如何实现滑动删除?”
只要您不想为delete按钮创建自定义UI,就可以利用SwiftUI并使用所有内置的特性。
ForEach有一个名为.onDelete的修饰符,它为您提供一个IndexSet。这表示当用户滑动时应该删除的行。现在,如果我们实现必要的逻辑并将其封装到一个动画块中,那么一切都将根据需要工作。
struct ContentView: View {
@State var cars = ["Tesla Model 3", "BMW i3", "Roadster", "Cybertruck", "Agera Koenigsegg", "Rimac Concept One"]
var body: some View {
NavigationView {
List {
ForEach(cars, id: \.self) { car in
Text(car)
}
.onDelete { indexSet in
withAnimation {
cars.remove(atOffsets: indexSet)
}
}
}
.navigationTitle("My Cars")
}
}
}注意事项:.onDelete修饰符是not,可与List一起使用,只能应用于ForEach。

“如何在SwiftUI中做两个手指的滑动手势?”
到目前为止,SwiftUI还不支持为多个手指创建手势。唯一的解决方案是结合使用UIViewRepresentable和UIPanGestureRecognizer。然后,您可以将minimumNumberOfTouches设置为2个手指。
这个来自苹果开发者论坛的帖子展示了一个简单的两个手指点击手势,你可以实现类似的东西,但是滑动的想法和概念是非常相似的,上面已经解释过了。
希望这能有所帮助!
https://stackoverflow.com/questions/62640289
复制相似问题