Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 876
davidev Avatar asked Dec 31 '22 02:12

davidev


1 Answers

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")
        }
    )
like image 75
Asperi Avatar answered Jan 10 '23 18:01

Asperi