SwiftUI:如何处理轻敲和轻敲长按按钮? [英] SwiftUI: how to handle BOTH tap & long press of button?
问题描述
我在 SwiftUI 中有一个按钮,我希望能够对点击按钮"(正常点击/点击)和长按"执行不同的操作.
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".
这在 SwiftUI 中可行吗?
Is that possible in SwiftUI?
这是我现在拥有的按钮的简单代码(仅处理正常"点击/触摸情况).
Here is the simple code for the button I have now (handles only the "normal" tap/touch case).
Button(action: {self.BLEinfo.startScan() }) {
Text("Scan")
} .disabled(self.BLEinfo.isScanning)
我已经尝试添加长按手势",但它仍然只执行"正常/短按"点击.这是我试过的代码:
I already tried to add a "longPress gesture" but it still only "executes" the "normal/short" click. This was the code I tried:
Button(action: {self.BLEinfo.startScan() }) {
Text("Scan")
.fontWeight(.regular)
.font(.body)
.gesture(
LongPressGesture(minimumDuration: 2)
.onEnded { _ in
print("Pressed!")
}
)
}
谢谢!
杰拉德
推荐答案
我尝试了很多东西,但最终我做到了:
I tried many things but finally I did something like this:
Button(action: {
}) {
VStack {
Image(self.imageName)
.resizable()
.onTapGesture {
self.action(false)
}
.onLongPressGesture(minimumDuration: 0.1) {
self.action(true)
}
}
}
仍然是一个有效果的按钮,只是短按和长按不同.
It is still a button with effects but short and long press are different.
这篇关于SwiftUI:如何处理轻敲和轻敲长按按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!