Search code examples
swiftgestureswiftui

How to detect EdgePan Gesture SwiftUI


Im trying to recognize a EdgePan from the left in SwiftUI. I know there are some gestures but haven't been able to apply them to the whole screen. (Tried with DragGesture). Is it possible to implement an EdgePan in SwiftUI? Or how can I use DragGesture to do the same?


Solution

  • Yeah, this is possible with a DragGesture.

    DragGesture has a startLocation property, which is a CGPoint. From this, you can detect where the gesture started, and using this you can determine if it started from an edge.

    Take your existing DragGesture, and in the .onChanged closure, pass in gesture, and find the start location with gesture.startLocation. Since you want to detect an edge, you'll want the x property of gesture.startLocation.

    It looks like this:

    DragGesture()
        .onChanged({gesture in
            if gesture.startLocation.x < CGFloat(100.0){
                print("edge pan")
            }
         }
    )