简体   繁体   English

SwiftUI ScrollView 也触发 DragGesture

[英]SwiftUI ScrollView also triggering DragGesture

I'm making swipeable flash cards that a user fills in with their own content.我正在制作用户用自己的内容填写的可滑动 flash 卡。 The card contains a vertical ScrollView (for text) and horizontal ScrollView (for tags).该卡片包含一个垂直 ScrollView(用于文本)和水平 ScrollView(用于标签)。 The card also has a drag gesture attached to it.该卡还附加了一个拖动手势。 However, when I'm scrolling through the ScrollViews it also triggers the DragGesture's.onChanged (but also doesn't even follow with.onEnded).但是,当我滚动浏览 ScrollViews 时,它也会触发 DragGesture's.onChanged(但也不会跟随 with.onEnded)。 How can I prioritize the ScrollView before the gesture?如何在手势之前优先考虑 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())
                }
            }
        }
    }
}

滚动滚动视图时激活卡片手势

try using gesture modifiers, such as simultaneously(with:), sequenced(before:) or exclusively(before:) to address you problem.尝试使用手势修饰符,例如同时(with:)、sequential(before:) 或exclusive(before:) 来解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM