TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Getting NavigationView and .onLongPressGesture to Play Nicely

Forums > SwiftUI

Hi all,

I'm struggling with something that seems like it should be pretty straightforward. I have a LazyVGrid that loads a number of images that each lead to a separate view via NavigationView. I'm trying to trigger two actions: 1) the normal NavigationView experience, and 2) on a long press, pop up a confirmationDialog with options to delete that image.

A code snippet below:

ScrollView {
    LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 4), content: {
          ForEach(images, id: \.self) { image in
              NavigationLink {
                  DetailView(image: image)
              } label: {
                  SmallImage(image: image, screenWidth: screenWidth, screenHeight: screenHeight)
                      .onLongPressGesture {
                          showingMore = true // trigger confirmationDialog
                      }
              }
          }
    })
}

There seems to be a gesture hierarchy that is eluding me - after adding in the .onLongPressGesture, the long press works but now the NavigationLink ignores a standard tap completely. Is there a recommended approach to setting that? I read through Paul's gestures how-to, but couldn't seem to apply the logic to NavigationLinks.

Any help appreciated!

2      

If I'm not mistaken, add a @State in the link.

NavigationLink(destination: ContentView(isActive: $isActive ), isActive: $isActive) {
    VStack(spacing: 0) {
    }
    .onTapGesture {
        print("2222")
    }
    .onLongPressGesture {
        $isActive.toggle()
    }
}

struct  ContentView() {

@Binding var isActive: Bool

  var body: some View {
      if  isActive {
        OloloView()
      } else {
        TrololoView()
      }
  }
}

2      

Quick update...I've been continuing through 100 Days of SwiftUI and got to the Context Menu lesson - exactly what I was looking for! Long press on a navigation link and boom, options appear.

Paul's easy to understand video here: https://www.hackingwithswift.com/books/ios-swiftui/creating-context-menus

2      

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.

Learn more here

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.