davidev
davidev

Reputation: 8547

SwiftUI TapGesture onStart / TouchDown

I am using the TapGesture in SwiftUI for MacOS. TapGesture is only recognized on TouchInsideOut event, when releasing the press again. I want to call an action on touchdown and another on the end gesture.

There is a onEnded state available for TapGesture but no onStart.

  MyView()
   .onTapGesture {
      //Action here only called after tap gesture is released
      NSLog("Test")
   }

Is there any chance to detect touch down and touch release?

I tried using LongPressGesture aswell, but couldn't figure it out.

Upvotes: 7

Views: 4055

Answers (2)

meaning-matters
meaning-matters

Reputation: 22966

The selected answer generates a continuous stream of events on touch, not just one on touch-down.

This works as requested:

MyView()
    .onLongPressGesture(minimumDuration: 0)
    {
        print("Touch Down")
    }

Upvotes: 4

Asperi
Asperi

Reputation: 258365

If by pure SwiftUI then only indirectly for now.

Here is an approach. Tested with Xcode 11.4.

Note: minimumDistance: 0.0 below is important !!

MyView()
    .gesture(DragGesture(minimumDistance: 0.0, coordinateSpace: .global)
        .onChanged { _ in
            print(">> touch down") // additional conditions might be here
        }
        .onEnded { _ in
            print("<< touch up")
        }
    )

Upvotes: 13

Related Questions