gpt4 book ai didi

SwiftUI ScrollView 也触发 DragGesture

转载 作者:行者123 更新时间:2023-12-04 13:56:32 24 4
gpt4 key购买 nike

我正在制作用户用他们自己的内容填写的可滑动闪存卡。该卡片包含一个垂直 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())
}
}
}
}
}

card gesture activated while scrolling through scrollview

最佳答案

尝试使用手势修饰符,例如同时(与:)、顺序(之前:)或专门(之前:)来解决您的问题。

关于SwiftUI ScrollView 也触发 DragGesture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61310048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com