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

List with selectable items and custom double click (tap) gesture

Forums > SwiftUI

On MacOS, I'm trying to create a List where I can click on items to select them, but I can also double click on them to do something else. As soon as I add tapGesture to my Text objects that I am using for items, their selection behavior breaks. I can no longer click on the actual text to select them, only in the empty space to the side of them. I understand the part of it that is explained here, but that doesn't solve my problem.

I tried to make something work with simultaneousGesture, as that's sort of what I'm trying to accomplish. But I need to retain the List's gesture functionality and add my own, not add two new ones.

Here is my code

struct ContentView: View {
    private var animals = ["frog","toad","owl","piglet","bear","roo"]
    @State private var selection = Set<String>()
    var body: some View {
        List(animals, id: \.self, selection: $selection, rowContent: { name in
            Text(name).onTapGesture(count: 2) {
                print("double clicked \(name)")
            }
        })
    }
}

Any help would be appreciated. And please, if what I am trying to accomplish isn't clear, let me know. Thank you for reading.

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.