UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

.onLongPressGesture on a ForEach List

Forums > SwiftUI

Can anyone help me find the correct syntax to add a LongPressGesture to my List? I would like to make it the Delete action.

List {
                            ForEach(products) { product in
                                NavigationLink(destination: ProductView(product: product)) { // <- Tap to edit Product
                                    HStack {
                                        Group {
                                            Text(product.name)
                                                .frame(maxWidth: .infinity, alignment: .leading)

                                            if let shoppingListProducts = shoppingList.products {
                                                Image(systemName: shoppingListProducts.contains(product) ? "circle.fill" : "circle")
                                            }
                                        }
                                    }
                                    // Swipe right to add Product to Shopping List
                                    .swipeActions( edge: .leading,
                                                   allowsFullSwipe: true) {
                                        Button("Add to list...") {
                                            add(product)
                                        }
                                        .tint(.yellow)
                                    }
                                    // Swipe left to remove Product from Shopping List
                                                   .swipeActions( edge: .trailing,
                                                                  allowsFullSwipe: false) {
                                                       Button("Remove from list...") {
                                                           remove(product)
                                                       }
                                                       .tint(.red)
                                                   }

                                }
                            }
                        }
                        .listStyle(.plain)

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.