Search code examples
swiftuiuigesturerecognizergesturelong-pressuilongpressgesturerecogni

SwiftUI - Make LongPressGesture remain when finger moves a bit?


I have a long-press gesture set up somewhat like this:

Text("Gesture text")
   .onLongPressGesture(minimumDuration: 5, pressing: {
          (isPressing){
            self.doingStuff()
       }

      }

and it works fine, but the issue is that if a user presses on it, holds it down, and then moves their finger ever so slightly, then the LongPressGesture stops being triggered.

I would like to somehow avoid this. Is there some way to do this? Maybe like increase the radius of the area of the LongPressGesture? This would especially be useful for people with larger fingers.

Thanks


Solution

  • You can use the maximumDistance parameter to define the area in which the callback will still be triggered

    Text("Gesture text")
       .onLongPressGesture(minimumDuration: 3, maximumDistance: 100) {
            print("Hello")
        }