使用DragGesture时ScrollView无法正常工作

时间:2020-09-05 15:51:38

标签: swiftui

我正在为LazyVGrid使用ScrollView。为了达到按下按钮的效果,我正在使用DragGesture。但是ScrollView无法正确滚动。

视觉表示:

enter image description here

这是我的代码:

struct ShowHint: View {
    
    @State var pressed: Int = -1
    var columns: [GridItem] = Array(repeating: .init(.flexible()), count: 5)
    
    var body: some View {
        
        ZStack{
            ScrollView(showsIndicators: false) {
                LazyVGrid(columns: columns, spacing: 30) {
                    ForEach(0..<500) { i in
                        Text("\(i)")
                            .padding(.vertical, 10)
                            .frame(maxWidth: .infinity)
                            .background(Color.red.opacity(pressed == i ? 0.5 : 1))
                            .gesture(DragGesture(minimumDistance: 0)
                                        .onChanged() { _ in
                                            pressed = pressed == i ? -1 : i }
                                        .onEnded { _ in
                                            pressed = -1
                                        })
                            .overlay(Group {
                                        if pressed == i {
                                            ShowOnTopOfButton(theS: "\(i)")
                                                .offset(y: -50.0)
                                                .allowsHitTesting(false)
                                        }})
                    }
                }
            }
            .padding(.top, 50)
            .padding(.horizontal, 10)
        }
    }
}


struct ShowOnTopOfButton: View {
    
    var theS: String
    var body: some View {
        VStack {
            Text("\(theS)")
                .padding(15)
                .background(Color.blue)
        }
    }
}

0 个答案:

没有答案