Search code examples
macoscocoaswiftuiappkit

How to Listen for trackpad events in MacOS SwiftUI app


I have a SwiftUI view that a user can drag to dismiss. On MacOS, this will translate the view if the user clicks and drags with their mouse. I'd like to allow them to swipe via the trackpad (similar to how the apple mail app can swipe messages). How do I do this with SwiftUI? What events do I listen for?

        .offset(self.offset)
        .gesture(
            DragGesture()
                .onChanged { gesture in
                    self.offset = gesture.translation
            }
        )

Solution

  • I ended up supporting this by overriding NSWindow.scrollWheel(with event:) that includes a call to event.trackSwipeEvent