0

I see there is a .onLongPressGesture(minimumDuration:maximumDistance:pressing:perform:).

However there isnt enough documentation for me to understand how to perform an action when the user releases the button.

A good example to understand what I am talking about is when you hold down a button to start recording a video and let go to stop the recording.

.onLongPressGesture(minimumDuration: 0.5, maximumDistance: 100, pressing: {}, perform: {} )
  • What if you just add `onTapGesture` to the `Button` in addition to `onLongPressGesture`? Also, I believe the `label` parameter is a `Label`, not a string. I believe you're looking for `Button(_:action:)`. – West1 May 19 '21 at 15:18
  • Does this answer your question https://stackoverflow.com/a/60465960/12299030? Or this one https://stackoverflow.com/a/61723144/12299030? – Asperi May 19 '21 at 15:30
  • Check this out: https://stackoverflow.com/a/61784292/14351818 – aheze May 19 '21 at 16:31
  • @West1 not relevant to the question –  May 19 '21 at 16:53
  • @BestFromAbove It was certainly relevant when I posted it (before you edited your question to exclude your incorrect use of `Button`). – West1 May 19 '21 at 17:19
  • @West1 Pointing out an incorrect label argument has no relevance to the question of an onLongPressGesture modifier. –  May 20 '21 at 13:55
  • @BestFromAbove 1) I regret trying to help you, lol. 2) Your code was never going to work as it was, so pointing this out was *absolutely* relevant. 3) In the future, you should test your code in Xcode before pasting it into Stack Overflow so you avoid making another stupid mistake. – West1 May 20 '21 at 14:29
  • @BestFromAbove Right. Well, have a nice day, pal. – West1 May 20 '21 at 16:40

1 Answers1

2

The answer to this is by calling the identifier like this.

  1. Created a state that tracks if the user has pressed the button
@State var hasPressed = false
  1. On the pressing parameter, use the state to perform an action depending on if the user is pressing the button or not.
.onLongPressGesture(minimumDuration: 0.5, maximumDistance: 100, pressing: {
                            pressing in
                            self.hasPressed = pressing
                            if pressing {action1}
                            if !pressing {action2}
                        }, perform: {})