read

For a simple view, we can use onTapGesture to handle taps easily. At times you want to handle simultaneous gestures.

I will use a common design pattern – a toast – for illustration.

A toast can have a call-to-action button for tapping. That can be done easily with a Button in the Toast.

At the same time, you might want the Toast to be dismissible by swiping up.

An implementation of a simple toast as such:

HStack {
    Text("Toast Text with Button")
    Button("Open") { ... } // Tap button to open
}
.offset(y: offset.height)
.simultaneousGesture(
    DragGesture()
        .onChanged { gesture in
            if gesture.translation.height < 0 { // Only for swipe up
                offset = gesture.translation
            }
        }
        .onEnded { _ in
            if abs(offset.height) > 30 {
                dismiss?() // Trigger a toast dismiss
            } else {
                offset = .zero
            }
        }
)

Image

@samwize

¯\_(ツ)_/¯

Back to Home