使用LongPressGesture多次触发操作

问题描述

我想在使用TapGesture时触发一次动作,而在使用LongPressGesture时多次触发动作。我想做的最好的例子是键盘上的“删除”键,长按它会继续删除最后一个字符。

我的问题是如何在SwiftUI中复制此行为。

目前,我想出了这个解决方案(不起作用):

struct ContentView: View {
    var longPress: some Gesture {
        LongPressGesture(minimumDuration: 3)
            .onChanged({ (bool) in
                guard bool else {
                    self.timer?.invalidate()
                    return
                }
                
                self.timer =  Timer.scheduledTimer(withTimeInterval: 0.2,repeats: true) { (timer) in
                     action() // Fired every 0.2 seconds until the user stop pressing
                }
            })
    }
    
    @State var timer: Timer? = nil

    var body: some View {
        Subview()
            .onTapGesture {
                action() // Fired once
            }
            .gesture(longPress)
    }
}

解决方法

您可以尝试以下操作:

public function test(Request $request)
{
    
    dd($request->route()->parameters());
}