本文介绍了任何相当于 UIKit 的“触摸"的 SwiftUI 按钮,即当您的手指触摸时激活按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于 SwiftUI,默认按钮行为相当于 UIKit 的touch up inside",当您的手指触摸按钮时激活,然后在按钮边界内抬起.
For SwiftUI the default button behavior is equivalent to UIKit's "touch up inside", which activates when your finger touches the button then raises while within the bounds of the button.
有什么办法可以将其更改为触摸",以便在您的手指触摸按钮时立即运行动作关闭?
Is there any way to change this to "touch down" so the action closure is run immediately when your finger touches the button?
推荐答案
您可以使用最小距离为零的 DragGesture 并为 DOWN (onChanged()) 或 UP (onEnded()) 定义闭包:
You can use a DragGesture with a minimumDistance of zero and define a closure for DOWN (onChanged()) or UP (onEnded()):
struct ContentView: View {
@State private var idx = 0
var body: some View {
let g = DragGesture(minimumDistance: 0, coordinateSpace: .local).onChanged({
print("DOWN: ($0)")
}).onEnded({
print("UP: ($0)")
})
return Rectangle().frame(width: 100, height: 50).gesture(g)
}
}
这篇关于任何相当于 UIKit 的“触摸"的 SwiftUI 按钮,即当您的手指触摸时激活按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!