SwiftUI: how to handle BOTH tap & long press of button?

前端 未结 8 1228
刺人心
刺人心 2020-12-15 06:59

I have a button in SwiftUI and I would like to be able to have a different action for \"tap button\" (normal click/tap) and \"long press\".

Is that possible in Swift

8条回答
  •  孤城傲影
    2020-12-15 07:57

    This isn't tested, but you can try to add a LongPressGesture to your button.

    It'll presumably look something like this.

    struct ContentView: View {
        @GestureState var isLongPressed = false
    
        var body: some View {
            let longPress = LongPressGesture()
                .updating($isLongPressed) { value, state, transaction in
                    state = value
                }
    
            return Button(/*...*/)
                .gesture(longPress)
        }
    }
    

提交回复
热议问题