任何与UIKit的"Touch down"等效的SwiftUI Button,即在您的手指触摸时激活按钮? [英] Any SwiftUI Button equivalent to UIKit's "touch down", i.e. activate button when your finger touches?

查看:94
本文介绍了任何与UIKit的"Touch down"等效的SwiftUI Button,即在您的手指触摸时激活按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于SwiftUI,默认按钮行为等效于UIKit的在内部向上触摸",当手指触摸按钮时会激活该按钮,然后在按钮范围内抬起该按钮.

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的"Touch down"等效的SwiftUI Button,即在您的手指触摸时激活按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆