swiftswiftuiuigesturerecognizer

How to make a LongPressGesture that runs repeatedly while the button is still being held down in SwiftUI?


I'd like to run the code in the longPressGesture every 0.5 seconds while the button is being held down. Any ideas on how to implement this?

import SwiftUI

struct ViewName: View {
    var body: some View {
        VStack {
            Button(action: { } ) {
            Image(systemName: "chevron.left")
            .onTapGesture {
                //Run code for tap gesture here
            }
            .onLongPressGesture (minimumDuration: 0.5) {
                //Run this code every 0.5 seconds
            }
        }
    }
}

Solution

  • You can do this by using timer. Make the timer starts when the user long pressed the image, and if the timer reaches 0, you can add two actions: 1. resetting the timer back to 0.5 seconds and 2.code you want to run every 0.5 seconds

        struct ContentView: View {
        @State var timeRemaining = 0.5
        let timer = Timer.publish(every: 0.5, on: .main, in: .common).autoconnect()
        @State var userIsPressing = false //detecting whether user is long pressing the screen
    
        var body: some View {
            VStack {
               Image(systemName: "chevron.left").onReceive(self.timer) { _ in
                   if self.userIsPressing == true {
                     if self.timeRemaining > 0 {
                        self.timeRemaining -= 0.5
                      }
                    //resetting the timer every 0.5 secdonds and executing code whenever //timer reaches 0
    
             if self.timeRemaining == 0 {
                    print("execute this code")
                    self.timeRemaining = 0.5
                 }
                }
            }.gesture(LongPressGesture(minimumDuration: 0.5)
                           .onChanged() { _ in
                               //when longpressGesture started
                           self.userIsPressing = true
                           }
                           .onEnded() { _ in
                               //when longpressGesture ended
                           self.userIsPressing = false
    
                           }
                           )
                   }
    }
    }