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
相关产品推荐
相关产品推荐

