0

Trying to see whether there is a way that item scroll can be detected on a ListView with CarouselListStyling (https://developer.apple.com/documentation/swiftui/carouselliststyle).

I am looking for an effect similar to Apple's calendar app, where when you scroll through your events, text in the header changes (The day and date changes when your events start on a different day).

enter image description here

Example of the code I have is:

VStack(alignment: .leading, spacing: 4) {
    Text("Text I want to update")
    
    List {
        ForEach(events, id: \.self) { event in
            EventCard(event: event)
        }
    }
    .listStyle(.carousel)
    
}

And I would like the text to be some State variable that will be updated when the list is scrolled.

In a perfect would there would just be a closure I could tap into something like:

@State var text: String = "hello"
....
VStack(alignment: .leading, spacing: 4) {
    Text(text)
    
    List {
        ForEach(events, id: \.self) { event in
            EventCard(event: event)
        }
    }
    .listStyle(.carousel)
    .onScroll { viewedRow in
        self.text = textForViewedRow(viewedRow)
    }
}

0 Answers0