iOS Development

ios – Making an attempt to detect which GridItem is at high of view SwiftUI

Spread the love


Certainly, the views are being reused. You must add a view that units the desire for each row of the grid.

.overlay {
    if gridItemIndex % 7 == 0 { // provides this for the primary view in each row
        GeometryReader { geometryProxy in
            Colour.clear
                .updateViewsYPosition(geometryProxy, gridItemIndex)
            
        }
    }
}

Now we have to implement the scale back methodology within the desire key, as a result of there will probably be a number of sibling views all setting their very own desire. The concept is, after decreasing every thing, the tip end result will point out the view that’s on the highest left.

Subsequently, we have to retailer each the body of the view (for decreasing) and the index of the view (in order that we will replace scrollPosition). We’ll use a kind like this for the desire key:

struct GridItemPosition: Equatable {
    let index: Int
    let body: CGRect
}

This is the reason we additionally move in gridItemIndex to updateViewsYPosition within the overlay.

The precise desire key can be carried out like this:

struct ScrollOffsetPreferenceKey: PreferenceKey {
    static var defaultValue = GridItemPosition(index: -1, body: .null)
    static func scale back(worth: inout GridItemPosition, nextValue: () -> GridItemPosition) {
        let subsequent = nextValue()
        if abs(worth.body.minY) > abs(subsequent.body.minY) {
            worth = subsequent
        }
    }
}

scale back is carried out in order that the desire is all the time the view with a y place that’s closest to 0. You’ll be able to change this standards to no matter you need.

In updateViewsYPosition, you need to get the body within the scrollView coordinate area.

func updateViewsYPosition(_ geometryProxy: GeometryProxy, _ i: Int) -> some View {
    let body = geometryProxy.body(in: .scrollView)
    return self.desire(
        key: ScrollOffsetPreferenceKey.self,
        worth: GridItemPosition(index: i, body: body)
    )
}

Full code:

struct ContentView: View {
    
    let gridRowLayout = Array(repeating: GridItem(spacing: 0), rely: 7)
    
    @State personal var scrollPosition: Int = 0
    
    var physique: some View {
        NavigationStack {
            ScrollView (.vertical){
                LazyVGrid(columns: gridRowLayout, spacing: 0){
                    ForEach(0..<1092, id: .self) { gridItemIndex in
                        Textual content("(abs(gridItemIndex))")
                            .overlay {
                                if gridItemIndex % 7 == 0 {
                                    GeometryReader { geometryProxy in
                                        Colour.clear
                                            .updateViewsYPosition(geometryProxy, gridItemIndex)
                                        
                                    }
                                }
                            }
                    }
                }
                .scrollTargetLayout()
                
                .onPreferenceChange(ScrollOffsetPreferenceKey.self) { worth in
                    self.scrollPosition = worth.index
                }
            }
            .navigationTitle("The High Row is: (scrollPosition)")
            .navigationBarTitleDisplayMode(.inline)
        }
        
    }
}

struct ScrollOffsetPreferenceKey: PreferenceKey {
    static var defaultValue = GridItemPosition(index: -1, body: .null)
    static func scale back(worth: inout GridItemPosition, nextValue: () -> GridItemPosition) {
        let subsequent = nextValue()
        if abs(worth.body.minY) > abs(subsequent.body.minY) {
            worth = subsequent
        }
    }
}

extension View {
    func updateViewsYPosition(_ geometryProxy: GeometryProxy, _ i: Int) -> some View {
        let body = geometryProxy.body(in: .scrollView)
        return self.desire(
            key: ScrollOffsetPreferenceKey.self,
            worth: GridItemPosition(index: i, body: body)
        )
    }
}

struct GridItemPosition: Equatable {
    let index: Int
    let body: CGRect
}

4 thoughts on “ios – Making an attempt to detect which GridItem is at high of view SwiftUI

  1. I loved you even more than you’ll say here. The picture is nice and your writing is stylish, but you read it quickly. I think you should give it another chance soon. I’ll likely do that again and again if you keep this walk safe.

  2. I loved you even more than you’ll say here. The picture is nice and your writing is stylish, but you read it quickly. I think you should give it another chance soon. I’ll likely do that again and again if you keep this walk safe.

  3. What i do not understood is in truth how you are not actually a lot more smartlyliked than you may be now You are very intelligent You realize therefore significantly in the case of this topic produced me individually imagine it from numerous numerous angles Its like men and women dont seem to be fascinated until it is one thing to do with Woman gaga Your own stuffs nice All the time care for it up

Leave a Reply

Your email address will not be published. Required fields are marked *