【发布时间】:2019-06-25 07:06:14
【问题描述】:
我正在尝试在 SwiftUI 中的按钮点击开始时实现触觉反馈。因此,我正在尝试使用同时手势,但我仍在苦苦挣扎。我不知道什么时候开始点击。
也没有为 Swift UI 实现触觉反馈,所以我想我会从 UIKit 中混合它?
我尝试实现 TapGesture 的更新方法,但它似乎没有做任何事情。这就是我到目前为止所得到的。感谢您的任何提示。
struct HapticButton : View {
@GestureState var isDetectingTap = false
var body: some View {
let tap = TapGesture()
.updating($isDetectingTap) { (body, stateType, transaction) in
// nothing happens below
print(body)
print(stateType)
print(transaction)
}.onEnded { _ in
// this one works but it is to late
// I need to figure out the beginning of the tap
print("Button was tapped, will invoke haptic feedback, maybe with UIKit")
}
return Button(action: {
print("Action executed")
}) {
HStack {
Image("icon")
Text("Login")
}
}.simultaneousGesture(tap)
}
}
【问题讨论】:
-
您可以在ButtonStyle中获取isPressed,查看示例here
-
对此有明确的解决方案吗? @mrsimply 是否包含 Victor 建议的示例代码?
标签: ios