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

SwiftUI:如何在Sheet上创建非阻塞DragGesture并保留交互关闭?

解决Sheet添加DragGesture后无法交互式关闭的问题

直接用simultaneousGesture替换原代码中的gesture即可实现需求,具体修改如下:

.sheet(isPresented: $sheetIsPresented) {
    ZStack {
        Rectangle()
            .scaledToFill()
            .foregroundColor(Color.red)
    }
    .simultaneousGesture(
        DragGesture(minimumDistance: 0).onChanged({ _ in
            print("Drag")
        }).onEnded({ _ in
            print("Drag ended")
        })
    )
}

原理说明

  • 原代码的gesture()方法会让自定义手势独占识别优先级,系统自带的Sheet交互式关闭手势会被拦截,导致无法拖拽关闭Sheet。
  • simultaneousGesture()允许自定义手势与系统手势同时被识别,既不会打断你需要的按住(onChanged触发)、释放(onEnded触发)事件检测,又能保留Sheet的默认交互式关闭功能,完全匹配你的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 17:22:35