WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

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.


Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.