Here’s a simple button that has three states: default, pressed and complete.
My definition of ‘complete’ here is when the long press gesture has reached its minimum duration. In this case I want to wait 3 seconds before the gesture is complete.
In each state I have a colour that I want the button to be. I am using two trailing closures in the onLongPressGesture
modifier: the first is called perform
, which tells the gesture what to do when the 3 second duration is complete. The second is labelled as onPressingChanged
, and it is passed a closure argument that tells me whether the button is currently being pressed.
Note that there is also a minimum distance that the finger can move after the touch has began. In other words, starting to long press on the button and then swiping to the top or bottom of the screen will cause the colour to change back to the default.
This is to prevent the button being activated by accidental gestures that were not centred around the button.