Why timer isn't working with button in SwiftUI

.onReceive construction isn't working with button long press gesture but tap is working, it increases count by 1. Construction should increase count every 0.5 seconds if button pressed

struct Test: View {
    @State private var count = 0
    @State private var isLongPressed = false

    var body: some View {
        VStack {
            Text("Count: \(count)")
            Button("Increase") {
                count += 1
                print("Button tapped")
            }
            .onLongPressGesture {
                isLongPressed = true
                print("Long press started")
            }
            .onLongPressGesture(minimumDuration: 0) {
                isLongPressed = false
                print("Long press ended")
            }
        }
        .onReceive(Timer.publish(every: 0.5, on: .main, in: .common).autoconnect()) { _ in
            if isLongPressed {
                count += 1
                print("Count increased: \(count)")
            }
        }
    }
}

@glebdolskiy Button automatically provides many of its own standard behaviors and its own gesture which it responds to when a user clicks or taps the button.

try adding a simultaneousGesture and see if that works.

struct Test: View {
    @State private var count = 0
    @State private var isLongPressed = false
    
    var body: some View {
        VStack {
            Text("Count: \(count)")
            Button("Increase") {
                count += 1
                print("Button tapped")
            }
            .simultaneousGesture(
                LongPressGesture(minimumDuration: 1)
                    .onEnded { _ in
                        isLongPressed = true
                        print("Long press started")
                        
                    }
            )
        }
    }
}
Why timer isn't working with button in SwiftUI
 
 
Q