How do you detect a SwiftUI touchDown event with no movement or duration?

后端 未结 6 826
无人及你
无人及你 2020-12-19 02:21

I\'m trying to detect when a finger first makes contact with a view in SwiftUI. I could do this very easily with UIKit Events but can\'t figure this out in SwiftUI.

I

6条回答
  •  半阙折子戏
    2020-12-19 03:12

    DragGesture was a bit unreliable for me, but LongPressGesture works perfectly. Here is a way to call methods when the GestureState value changes:

    @GestureState var longPressGestureState = false
    
    var isTouchedDown: Bool {
        // use this place to call functions when the value changes
    
        return longPressGestureState
    }
    
    var body: View {
        Color(isTouchedDown ? .red : .black)
            .gesture(
                LongPressGesture(minimumDuration: .infinity, maximumDistance: .infinity)
                    .updating($longPressGestureState) { value, state, _ in
                        state = value
                    }
            )
    }
    

提交回复
热议问题