SwiftUI 列表 &出现 [英] SwiftUI Lists & OnAppear

查看:22
本文介绍了SwiftUI 列表 &出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到列表视图的 OnAppear 事件有一些奇怪的行为.我认为 OnAppear 闭包会在屏幕上出现视图时运行,但它似乎在加载列表时立即运行.

I have noticed some odd behavior on OnAppear events for List views. I'd think the OnAppear closure would run whenever a view appears on the screen, but it appears to run all at once when the List is loaded.

例如以下代码:

@State var rows: [String] = Array(repeating: "Item", count: 20)

var body: some View {

    List(0..<rows.count, id: \.self) { index in
        Text(verbatim: self.rows[index])
            .onAppear {
                print("BOOOOM")
            }
            .frame(height:400)
    }
}

...我希望打印命令在加载时运行几次,然后在我向下滚动时继续打印.相反,它一次打印 20 次,然后在我开始向下滚动时再次打印.

...I would expect the print command to run a couple times on load, then continue to print as I scrolled down. Instead it prints 20 times at once, then again as I start to scroll down.

有什么想法吗?

推荐答案

我认为它的行为符合预期.

I think it is behaving as expected.

对我来说,它在带有 iPhone 7 的模拟器上打印了 15 次,在带有 iPhone 11 的模拟器上打印了 20 次.

For me it printed 15 times on a Simulator with iPhone 7 and all 20 times on a Simulator with an iPhone 11.

我对 print("BOOOOM \(index)")

列表在后台遵守的性能和资源可能存在平衡.

There is probably a balance with performance and resources that the List abides by in the background.

加载太少,如果滚动太快与加载太多并减慢滚动动画,用户将卡住".

Load too little and the user will "Get stuck" if scrolling too fast vs loading too much and slowing the scroll animation.

这篇关于SwiftUI 列表 &amp;出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆