SwiftUI ScrollView 也触发 DragGesture
Posted
技术标签:
【中文标题】SwiftUI ScrollView 也触发 DragGesture【英文标题】:SwiftUI ScrollView also triggering DragGesture 【发布时间】:2020-04-19 19:04:54 【问题描述】:我正在制作用户用他们自己的内容填写的可滑动的闪存卡。该卡片包含一个垂直 ScrollView(用于文本)和水平 ScrollView(用于标签)。该卡还附加了一个拖动手势。但是,当我滚动浏览 ScrollViews 时,它也会触发 DragGesture 的 .onChanged(但也不会跟随 .onEnded)。如何在手势之前优先考虑 ScrollView?
struct ContentView: View
@State private var translation: CGSize = .zero
var body: some View
GeometryReader geometry in
ZStack
Color(.blue).edgesIgnoringSafeArea(.all)
VStack
ScrollView
Text("blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah")
Tags(decks: ["Tag", "Tag", "Tag", "Tag", "Tag", "Tag", "Tag"])
.padding(24)
.frame(width: geometry.size.width - 48, height: geometry.size.height / 2)
.background(Color.gray)
.clipShape(RoundedRectangle(cornerRadius: 18, style: .continuous))
.animation(.interactiveSpring(response: 0.5, dampingFraction: 0.75, blendDuration: 0))
.offset(x: self.translation.width, y: self.translation.height)
.gesture(DragGesture()
.onChanged value in
self.translation = value.translation
.onEnded value in
self.translation = .zero
)
struct ContentView_Previews: PreviewProvider
static var previews: some View
ContentView()
struct Tags: View
var decks: [String] = []
var body: some View
ScrollView(.horizontal, showsIndicators: false)
HStack
ForEach(decks, id: \.self) deck in
Text(deck)
.font(.system(size: 12, weight: .semibold))
.padding(.vertical, 6)
.padding(.horizontal, 12)
.background(Color.white)
.clipShape(Capsule())
【问题讨论】:
你找到解决这个问题的方法了吗? @erkutbas 不幸的是我没有 【参考方案1】:尝试使用手势修饰符,例如同时(with:)、sequential(before:) 或exclusive(before:) 来解决您的问题。
【讨论】:
如果我了解我在网上找到的内容,那是我自己实现的两个手势,但我只添加了一个 DragGesture。但是,我认为 ScrollView 内置了某种手势,但我不确定如何引用它以在您建议的修饰符中使用它。以上是关于SwiftUI ScrollView 也触发 DragGesture的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI - 调整 ScrollView 滚动指示器插图