SwiftUI:按下按钮时的动作

时间:2020-05-11 04:32:14

标签: swiftui

我试图实现一个长按按钮,该按钮仅在按下按钮时才会更改我正在使用的SF符号。我不确定在哪里也可以使用图片。问题是当我停止按时,按钮被触发。有没有办法不激活它(按下时除外)?

我正在寻找的按钮是:

按下 2秒钟,符号将更改,在这2秒钟后,释放后,符号返回< strong>初始状态。

{ multi : true }

1 个答案:

答案 0 :(得分:2)

这是一个解决方案。经过Xcode 11.5b的测试。

demo

struct heartButton: View {

    @GestureState private var isDetectingPress = false

    var body: some View {
        VStack {
            Image(systemName: isDetectingPress ? "heart.fill" : "heart").font(.system(size: 16, weight: .regular))
            Text("Press")
            .gesture(LongPressGesture(minimumDuration: 2).sequenced(before: DragGesture(minimumDistance: 0, coordinateSpace: .local)).updating($isDetectingPress) { value, state, _ in
                    switch value {
                        case .second(true, nil):
                            state = true
                        default:
                            break
                    }
            })
        }
    }
}