You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET Maui中修改DragGestureRecognizer的默认按压识别时间?

修改DragGestureRecognizer按压识别时间的简便方案

不用自定义手势识别器或TouchBehavior,你可以直接使用Flutter内置的DelayedDragGestureRecognizer,它专门用于控制拖拽触发前的延迟时长,完全满足你的需求。

实现步骤:

  • 创建DelayedDragGestureRecognizer实例,通过delay参数设置你需要的按压识别时间(比如Duration(milliseconds: 500),即按住500毫秒后才触发拖拽)。
  • 将该识别器添加到RawGestureDetector中使用,或结合GestureDetector的自定义手势识别器配置。

示例代码:

RawGestureDetector(
  gestures: {
    DelayedDragGestureRecognizer: GestureRecognizerFactoryWithHandlers<DelayedDragGestureRecognizer>(
      () => DelayedDragGestureRecognizer(
        delay: Duration(milliseconds: 500), // 设置按压识别延迟时间
      ),
      (instance) {
        instance.onStart = (DragStartDetails details) {
          // 拖拽开始逻辑
          print("拖拽开始");
        };
        instance.onUpdate = (DragUpdateDetails details) {
          // 拖拽更新逻辑
          print("拖拽偏移: ${details.delta}");
        };
        instance.onEnd = (DragEndDetails details) {
          // 拖拽结束逻辑
          print("拖拽结束");
        };
      },
    ),
  },
  child: Container(
    width: 200,
    height: 200,
    color: Colors.blue,
    child: Center(child: Text("按住500ms后拖拽")),
  ),
)

补充说明:

  • DelayedDragGestureRecognizer是Flutter SDK内置组件,无需额外依赖,比自定义识别器或TouchBehavior更轻量简便。
  • 如果需要区分水平/垂直拖拽,可使用DelayedHorizontalDragGestureRecognizer或DelayedVerticalDragGestureRecognizer,二者同样支持delay参数设置。

内容的提问来源于stack exchange,提问作者Marcos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 14:22:08