SwiftUI - 如何检测长按按钮? [英] SwiftUI - how to detect long press on Button?

查看:56
本文介绍了SwiftUI - 如何检测长按按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,当它被按下时,它会执行一些操作.但我想修改相同的 Button 以检测更长的按下时间,并执行一组不同的过程.如何修改此代码以检测长按?

I have a Button where when it gets pressed, it performs some actions. But I would like to modify the same Button to detect a longer press, and perform a different set of processes. How do I modify this code to detect a long press?

Button(action: {
              // some processes

            }) {
              Image(systemName: "circle")
                .font(.headline)
                .opacity(0.4)

            }

推荐答案

这里是可能的变体(使用 Xcode 11.2/iSO 13.2 测试).

Here is possible variant (tested with Xcode 11.2 / iSO 13.2).

Button("Demo") {
    print("> tap")
}
.simultaneousGesture(LongPressGesture().onEnded { _ in
    print(">> long press")
})

这篇关于SwiftUI - 如何检测长按按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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