Issue with PasteButton and Background Gesture Conflict in SwiftUI

Hi everyone! I've encountered an issue while developing an app using SwiftUI and could use some help or suggestions. It seems that there's a conflict between using PasteButton and a background gesture simultaneously.

Xcode: 14.3.1

    var body: some View {
        VStack(spacing: 25) {
            Button("button") {
                debugPrint("tap button")
            }
            
            PasteButton(payloadType: String.self, onPaste: { _ in
                debugPrint("tap PasteButton")
            })
        }
        .background(.yellow)
        .onTapGesture {
            debugPrint("tap background")
        }
    }

In the code snippet above, when I tap the Button, it displays tap button. However, when I tap the PasteButton, it shows tap background. Strangely, if I long-press for a second and then release, it displays tap PasteButton.

From the view hierarchy perspective, Button and PasteButton both appear at the top level. The only peculiar aspect is that upon screen generation, the PasteButton doesn't immediately appear; it gradually emerges with an animation after approximately 0.5 seconds.

If anyone has experience or thoughts on handling this PasteButton and background gesture issue in SwiftUI, I'd greatly appreciate your input. Thanks so much!

Hi @clydehsieh ,

Have you tried using .simultaneousGesture? This allows you to have two gestures at the same time, so neither the PasteButton or the background gesture will take precedence. Your code would look more like this:

let tapGesture = TapGesture().onEnded {
            debugPrint("tap background")
        }
var body: some View {
        VStack(spacing: 25) {
            Button("button") {
                debugPrint("tap button")
            }
            
            PasteButton(payloadType: String.self, onPaste: { _ in
                debugPrint("tap PasteButton")
            })
        }
        .background(.yellow)
        .simultaneousGesture(tapGesture)
    }

@clydehsieh I see the problem! Your PasteButton initializer is init(payloadType:onPaste:) - found here https://developer.apple.com/documentation/swiftui/pastebutton/init(payloadtype:onpaste:) This creates a PasteButton that only prints the message "tap PasteButton" when something is pasted since that message is passed to the onPaste parameter. Instead, you want to print the message immediately, so put it in the button's closure without the onPaste parameter.

For example:

PasteButton(payloadType: String.self) { _ in
           debugPrint("tap PasteButton")
        }

More info is shown here https://developer.apple.com/documentation/swiftui/pastebutton

After various attempts, the final decision was to handle it using a workaround approach, by utilizing ZStack to place the background and PasteButton in different view stacks.

    var body: some View {
        ZStack {
            Color.yellow
                .frame(maxWidth: .greatestFiniteMagnitude, maxHeight: .greatestFiniteMagnitude)
                .onTapGesture {
                    debugPrint("tap background")
                }
            VStack(spacing: 25) {
                Button("button") {
                    debugPrint("tap button")
                }
                
                PasteButton(payloadType: String.self, onPaste: { _ in
                    debugPrint("tap PasteButton")
                })
            }
        }
    }
Issue with PasteButton and Background Gesture Conflict in SwiftUI
 
 
Q