【问题标题】:How to run function when long press gesture detected and stopped?检测到长按手势并停止时如何运行功能?
【发布时间】:2021-02-25 10:29:09
【问题描述】:

我想实现以下示例:用户需要按下一个按钮至少 0.3 秒长,然后录音开始,用户松开按钮后录音停止。

我的解决办法是:

Image(systemName: "mic.circle")
            .resizable()
            .aspectRatio(contentMode: .fit)
            .frame(width: 70)
            .foregroundColor(Color.blue)
            .onLongPressGesture(minimumDuration: 0.3){
                print("start recording...")
            }

当用户停止按下按钮时,如何实现该功能?

【问题讨论】:

标签: swiftui long-press


【解决方案1】:

我可以解决我的问题,希望它对你们中的一些人有用:

@State private var isPressingDown: Bool = false

Image(systemName: "mic.circle")
        .resizable()
        .aspectRatio(contentMode: .fit)
        .frame(width: 70)
        .foregroundColor(Color.blue)
        .onLongPressGesture(minimumDuration: 0.3){
            self.isPressingDown = true
            print("started")
        }
        .simultaneousGesture(
            DragGesture(minimumDistance: 0)
                .onEnded{ _ in
                    if self.isPressingDown{
                        self.isPressingDown = false
                            print("ended")
                        }
                    }
            )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多