Text .onTapGesture Never Called When Shown with .onLongPressGesture

I'm showing a Text View when a button with an image is long-pressed.

import SwiftUI

struct ContentView: View {
    @Environment(\.colorScheme) var colorScheme
    var isDark: Bool {
        return colorScheme == .dark
    }
    @State private var showLabel = false
    
    var body: some View {
        Button(action: {
            
        }) {
            VStack {
                ZStack {
                    Image(systemName: "swift")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 32)
                        .padding(.horizontal, 40)
                        .padding(.vertical, 6)
                        .background(.gray.opacity(0.2), in: RoundedRectangle(cornerRadius: 10))
                        .onTapGesture {
                            showLabel.toggle()
                        }
                        .onLongPressGesture(minimumDuration: 2) {
                            print("Long pressed...")
                            showLabel.toggle()
                    }
                    if showLabel {
                        Text("Help Content")
                            .font(.caption)
                            .foregroundStyle(!isDark ? .white : .black)
                            .padding(10)
                            .background(!isDark ? .black : .white, in: Rectangle())
                            .onTapGesture {
                                print("hey")
                                showLabel.toggle()
                            }
                            .offset(x: 120)
                    }
                }
            }
        }
    }
}

So a Text View will appear as shown in the image above. But its .onTapGesture is never called. I wonder why? Thanks.

Answered by Vision Pro Engineer in 796495022

Hi @Tomato ,

Have you tried using simultaneousGesture? This allows you to use two gestures at the same time.

Best,

Sydney

Accepted Answer

Hi @Tomato ,

Have you tried using simultaneousGesture? This allows you to use two gestures at the same time.

Best,

Sydney

Text .onTapGesture Never Called When Shown with .onLongPressGesture
 
 
Q