GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

How to correctly implement .onAppear and LazyVStack?

Forums > SwiftUI

Hi, i have created a playground and tried to show the behavior. I want to create a tik tok style scrollview but with Text instead of Videos. I want to trigger the .onAppear after scrolling to the next Text. The problem is that the .onAppear doesn't get triggered every time when I scroll to the next Text. I think it is a issue with the LazyVstack because it does not immediately unload the texts after scrolling. Would like to know if anyone has a solution for this.

My test code:

   ZStack{
            RoundedRectangle(cornerRadius: 18)
                .foregroundStyle(Color.gray)
                .frame(width: 380,height: 440)
            ScrollView(showsIndicators: false){
                LazyVStack(spacing:0){
                    Text("THis is A Test THis is a test this is a Test. This is a Test. THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.THis is A Test THis is a test this is a Test. This is a Test.")
                        .onAppear{
                            print("1")
                        }
                        .background(Color.red)
                        .frame(width: 380,height: 440, alignment: .topLeading)
                        .background(Color.green)

                    Text("Hello, World!")
                        .onAppear{
                            print("2")
                        }
                        .background(Color.red)
                        .frame(width: 380,height: 440, alignment: .topLeading)
                        .background(Color.green)
                    Text("Hello, World!")
                        .onAppear{
                            print("3")
                        }
                        .background(Color.red)
                        .frame(width: 380,height: 440, alignment: .topLeading)
                        .background(Color.green)
                    Text("Hello, World!")
                        .onAppear{
                            print("4")
                        }
                        .background(Color.red)
                        .frame(width: 380,height: 440, alignment: .topLeading)
                        .background(Color.green)
                }
            }
            .scrollTargetBehavior(.paging)
        }
        .frame(width: 380,height: 440)

2      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.