TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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 Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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

Reply to this topic…

You need to create an account or log in to reply.

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.