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

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.