Xcode16中嵌套DragGesture的ScrollView无法垂直滚动问题求助
问题:Xcode 16构建后垂直ScrollView/List内子视图水平拖拽导致无法上下滚动
自Xcode 16起,垂直ScrollView或List组件内,带有水平DragGesture的子视图会出现交互冲突:子视图能正常识别水平拖拽,但容器的垂直滚动完全无法触发。
已尝试的方案及问题:
- 使用
.gesture或.highPriorityGesture绑定水平拖拽手势:ScrollView/List垂直滚动完全失效 - 使用
.simultaneousGesture:DragGesture与TapGesture会同时触发,不符合预期交互逻辑
补充说明:
- 该问题仅存在于Xcode 16构建的应用中
- Xcode 15构建的应用在iOS 18上运行无此问题
- 目前苹果开发者论坛相关讨论暂未给出有效解决方案
子视图代码(RowItem)
struct RowItem: View { let item: Int @State var offsetWidth: CGFloat = 0.0 var body: some View { HStack { Text("Row \(item)") Spacer() } .padding() .background(Color.gray) .offset(x: offsetWidth) .gesture( // 问题触发点 DragGesture() .onChanged { gesture in let width = gesture.translation.width if -100..<0 ~= width { if self.offsetWidth != -100 { self.offsetWidth = width } } else if width < -100 { self.offsetWidth = -100 } } .onEnded { _ in if self.offsetWidth > -50 { self.offsetWidth = .zero } else { self.offsetWidth = -100 } } ) .onChange(of: offsetWidth) { newVal in Timer.scheduledTimer(withTimeInterval: 2, repeats: false) { _ in withAnimation { offsetWidth = 0.0 } } } } }
滚动容器代码(ExperimentView)
struct ExperimentView: View { var body: some View { ScrollView { LazyVStack { ForEach(1...100, id: \.self) { i in RowItem(item: i) .background(Color.red) } } } } }
内容的提问来源于stack exchange,提问作者Shri
相关产品推荐
相关产品推荐

