0

I am trying to get familiar with LazyVGrid, layout and frames/coordinate spaces in SwiftUI and draw a grid that has 4 columns, each being 1/4 the width of the screen. On top of that, on cell tap, I want to place a view exactly over the tapped cell and animate it to full screen (or a custom frame of my choice).

I have the following code:

struct CellInfo {
    let cellId:String
    let globalFrame:CGRect
}


struct TestView: View {
    
    
    var columns = [
        GridItem(.flexible(), spacing: 0),
        GridItem(.flexible(), spacing: 0),
        GridItem(.flexible(), spacing: 0),
        GridItem(.flexible(), spacing: 0)
    ]
    
    let items = (1...100).map { "Cell \($0)" }
    
    
    @State private var cellInfo:CellInfo?
    
    var body: some View {
        
        GeometryReader { geoProxy in
            
            let cellSide = CGFloat(Int(geoProxy.size.width) / columns.count)
            let _ = print("cellSide: \(cellSide). cellSide * columns: \(cellSide*CGFloat(columns.count)), geoProxy.size.width: \(geoProxy.size.width)")
            
            ZStack(alignment: .center) {
                
                ScrollView(.vertical) {

                    LazyVGrid(columns: columns, alignment: .center, spacing: 0) {

                        ForEach(items, id: \.self) { id in
                            
                                CellView(testId: id, cellInfo: $cellInfo)
                                    .background(Color(.green))
                                    .frame(width: cellSide, height: cellSide, alignment: .center)
                            
                        }

                    }

                }
                .clipped()
                .background(Color(.systemYellow))
                .frame(maxWidth: geoProxy.size.width, maxHeight: geoProxy.size.height)
                
                
                
                if cellInfo != nil {
                    
                    Rectangle()
                        .background(Color(.white))
                        .frame(width:cellInfo!.globalFrame.size.width, height: cellInfo!.globalFrame.size.height)
                        .position(x: cellInfo!.globalFrame.origin.x, y: cellInfo!.globalFrame.origin.y)
                    
                }
                
                
            }
            .background(Color(.systemBlue))
            .frame(maxWidth: geoProxy.size.width, maxHeight: geoProxy.size.height)
            
            
        }
        .background(Color(.systemRed))
        .coordinateSpace(name: "testCSpace")
        .statusBar(hidden: true)
        
    }
    
    
}



struct CellView: View {
    
    @State var testId:String
    @Binding var cellInfo:CellInfo?
    
    var body: some View {
        
        GeometryReader { geoProxy in
            
            ZStack {
                
                Rectangle()
                    
                    .stroke(Color(.systemRed), lineWidth: 1)
                    .background(Color(.systemOrange))
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .overlay(
                        Text("cell: \(testId)")
                    )
                    .onTapGesture {
                        
                        let testCSpaceFrame = geoProxy.frame(in: .named("testCSpace"))
                        let localFrame = geoProxy.frame(in: .local)
                        let globalFrame = geoProxy.frame(in: .global)
                        print("local frame: \(localFrame), globalFrame: \(globalFrame), testCSpace: \(testCSpaceFrame)")
                        let info = CellInfo(cellId: testId, globalFrame: globalFrame)
                        print("on tap cell info: \(info)")
                        cellInfo = info
                        
                    }
                
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            
        }
        .background(Color(.systemGray))
        
    }
    
}

The outermost geometry proxy gives this size log:

cellSide: 341.5. cellSide * columns: 1366.0, geoProxy.size.width: 1366.0

This is what gets rendered:

enter image description here

When I tap on cell 1 for example, this is what gets logged:

local frame: (0.0, 0.0, 341.0, 341.0), globalFrame: (0.25, 0.0, 341.0, 341.0), testCSpace: (0.25, 0.0, 341.0, 341.0) on tap cell info: CellInfo(cellId: "Cell 1", globalFrame: (0.25, 0.0, 341.0, 341.0))

When tapping on "Cell 6" this is what the newly rendered screen looks like:

enter image description here

So, given this code:

  1. how can I get the (white) overlaid view's frame to match the cell's view's frame I tapped on? The width and height seem ok, but the position is off. (What am I doing wrong?)
  2. How can I get the white view to animate to full screen once placed right on top of the tapped cell?
zumzum
  • 17,984
  • 26
  • 111
  • 172
  • You really might want to [consider matchedGeometryEffect](https://stackoverflow.com/questions/68304812/swiftui-get-views-frame-on-tap#comment120718738_68304812). GeometryReader + SwiftUI animations can only go so far... but I'm also interested if what you want is possible with just those – aheze Jul 12 '21 at 19:05
  • I have, I posted a couple of questions about that, one of them is this one https://stackoverflow.com/questions/68305767/swiftui-present-view-growing-out-of-tapped-view-and-dismiss-shrinking-back-to-v , however, I'd like to know the answer to both methods and compare. – zumzum Jul 12 '21 at 19:15

0 Answers0