Show hint view in LazyVGrid in SwiftUI

Viewed 495

I have lots of button in a LazyVGrid in a ScrollView. I am trying to show a hint view just top of the button I clicked (as like keyboard popup). I don't know how do I catch the position of a ScrollView button. Besides need help to select suitable gesture to complete the task.

Graphical representation...

enter image description here

Here is my code:

struct ShowHint: View {
    
    @State var isPressed: Bool = false
    var columns: [GridItem] = Array(repeating: .init(.flexible()), count: 5)
    
    var body: some View {
    
        ZStack{
            
            if isPressed {
                ShowOnTopOfButton().zIndex(1)
            }
            
            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( isPressed ? 0.5 : 0.9))
                            .gesture(TapGesture()
                                        //.onStart { _ in isPressed = true } //but there is no property like this!
                                        .onEnded { _ in isPressed = !isPressed }
                            )
                    }
                }
            }
            .padding(.top, 50)
            .padding(.horizontal, 10)
        }
    }
}


struct ShowOnTopOfButton: View {
    
    var theS: String = "A"
    var body: some View {
        VStack {
            Text("\(theS)")
                .padding(20)
                .background(Color.blue)
        }
    }
}
1 Answers

Here is possible solution - the idea is to show hint view as overlay of tapped element.

Tested with Xcode 12 / iOS 14

demo

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 : 0.9))
                            .gesture(TapGesture()
                                .onEnded { _ in pressed = pressed == i ? -1 : i }
                            )
                            .overlay(Group {
                                if pressed == i {
                                    ShowOnTopOfButton()
                                        .allowsHitTesting(false)
                                }}
                            )
                    }
                }
            }
            .padding(.top, 50)
            .padding(.horizontal, 10)
        }
    }
}
Related