I’ve created a repo with one demonstration venture
https://github.com/PavelKatunin/SectionsAndItems
The entire thought and necessities are described within the readme file.
Lengthy story quick: it’s an infinity vertical feed, and I need to have the ability to rely reliably what number of sections and gadgets within the feed a consumer has seen.
I’ve concepts for 3 variations of implementation of this performance,
however I began with because it appeared in the mean time probably the most easy one.
I wished to trace look and disappearance of Sections and Gadgets by dealing with
onAppear and onDisappear callbacks.
And I discovered that typically onDisappear isn’t referred to as when a view really disappears from the display screen and likewise there isn’t any logical order of the calls (because it appears to me).
So I’m wondering if I am doing one thing incorrect right here?
Here’s a log after I scroll via the feed:
SiwftUI occasion: Part 1 appeared
SiwftUI occasion: Merchandise 1 appeared
SiwftUI occasion: Merchandise 2 appeared
SiwftUI occasion: Merchandise 1 disappeared
SiwftUI occasion: Merchandise 3 appeared
SiwftUI occasion: Merchandise 2 disappeared
SiwftUI occasion: Merchandise 4 appeared
SiwftUI occasion: Part 2 appeared
SiwftUI occasion: Merchandise 1 appeared
SiwftUI occasion: Merchandise 4 disappeared
SiwftUI occasion: Merchandise 3 disappeared
SiwftUI occasion: Merchandise 2 appeared
SiwftUI occasion: Merchandise 1 disappeared
SiwftUI occasion: Merchandise 3 appeared
SiwftUI occasion: Part 1 disappeared
SiwftUI occasion: Part 3 appeared
SiwftUI occasion: Merchandise 1 appeared
SiwftUI occasion: Merchandise 3 disappeared
SiwftUI occasion: Merchandise 2 disappeared
SiwftUI occasion: Merchandise 2 appeared
SiwftUI occasion: Merchandise 1 disappeared
SiwftUI occasion: Merchandise 3 appeared
SiwftUI occasion: Part 2 disappeared
SiwftUI occasion: Merchandise 2 disappeared
SiwftUI occasion: Merchandise 4 appeared
Right here is my 3 easy views:
struct ItemView: View {
@State var textual content: String
var physique: some View {
ZStack {
Textual content("(textual content)").font(.title)
}
.onAppear() {
print("SiwftUI occasion: Merchandise (textual content) appeared")
}
.onDisappear() {
print("SiwftUI occasion: Merchandise (textual content) disappeared")
}
}
}
struct SectionView: View {
@State var shade: Shade
@State var gadgets: [String]
var sectionId: String
var physique: some View {
LazyVStack(spacing: 0) {
ForEach(gadgets, id: .hashValue) { merchandise in
ItemView(textual content: merchandise)
.body(top: UIScreen.fundamental.bounds.dimension.top)
}
}
.onAppear() {
print("SiwftUI occasion: Part (sectionId) appeared")
}
.onDisappear() {
print("SiwftUI occasion: Part (sectionId) disappeared")
}
.background(shade)
}
}
struct ScrollOffsetFeedView: View {
static func createLocalSections() -> [Section] {
return [Section(id: "1", color: .green, items: ["1", "2", "3", "4"]),
Part(id: "2", shade: .crimson, gadgets: ["1", "2", "3"]),
Part(id: "3", shade: .blue, gadgets: ["1", "2", "3", "4", "5"])]
}
@State var sections: [Section]
init(sections: [Section]) {
self.sections = sections
}
var physique: some View {
ZStack {
ScrollView {
LazyVStack(spacing: 0) {
ForEach(sections, id: .id) { part in
SectionView(shade: part.shade,
gadgets: part.gadgets,
sectionId: part.id)
}
}
.scrollTargetLayout()
}
.scrollTargetBehavior(.paging)
.ignoresSafeArea()
.scrollIndicators(.hidden)
VStack {
ZStack {
makeSectionsCounter()
.background(Shade(white: 0.0, opacity: 0.5))
.cornerRadius(10)
.padding()
}
Spacer()
}
}
}
@ViewBuilder func makeSectionsCounter() -> some View {
ZStack {
Textual content("Sections accomplished 0, merchandise 1 / 4")
}
.padding()
}
}
How does onAppear and onDisappear work in ScrollView and LazyVStack?