{"id":4901,"date":"2022-01-16T09:16:10","date_gmt":"2022-01-16T01:16:10","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4901"},"modified":"2022-01-16T09:16:10","modified_gmt":"2022-01-16T01:16:10","slug":"swiftu2-0-lazyvstack-lazyhstack","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4901","title":{"rendered":"SwiftU2.0 LazyVStack LazyHStack"},"content":{"rendered":"\n<p>SwiftUI2.0 \u63d0\u4f9b\u4e86 LazyVStack \u548c LazyHStack\uff0c\u5176\u4f5c\u7528\u662f\u53ea\u6709\u5f53 View \u5728\u53ef\u89c1\u533a\u57df\u5185\u624d\u8fdb\u884c\u6e32\u67d3\uff0c\u8fd9\u6837\u53ef\u4ee5\u5927\u5927\u5927\u63d0\u9ad8 app \u6267\u884c\u6548\u7387\u3002\u7531\u4e8e VStack \u6216 HStack \u5bfc\u81f4\u7684\u6548\u7387\u95ee\u9898\uff0c\u5728 <a rel=\"noreferrer noopener\" href=\"https:\/\/zhuanlan.zhihu.com\/p\/111151515\" target=\"_blank\">SwiftUI List (3) \u2014\u2014 List\u3001Form\u3001VStack<\/a> \u6587\u7ae0\u4e2d\u6709\u7b80\u5355\u7684\u6bd4\u8f83\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u57fa\u672c\u7528\u6cd5<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct LazyStack: View {\n    var body: some View {\n        ScrollView{\n            LazyVStack{ \/\/\u6362\u6210 VStack \u4f5c\u6bd4\u8f83\u65b0\u6570\u636e\u521b\u5efa\u7684\u65f6\u673a\n                ForEach(0...1000,id:\\.self){ id in\n                    Text(LazyItem(id:id).title)\n                }\n            }\n        }\n    }\n}\n\nstruct LazyItem{\n    let id:Int\n    let title:String\n    init(id:Int){\n        self.id = id\n        self.title = \"id:\\(id)\"\n        print(\"init new object:\\(id)\") \n    }\n}<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u4f7f\u7528 Lazy \u7279\u6027\u521b\u5efa\u4e0d\u95f4\u65ad\u7684\u5217\u8868\u663e\u793a<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import SwiftUI\n\nstruct LazyStack: View {\n    @State var list = (0...40).map{_ in Item(number:Int.random(in: 1000...5000))}\n    @State var loading = false\n    var body: some View {\n        VStack{\n        Text(\"count:\\(list.count)\")\n        \/\/\u6570\u636e\u6570\u91cf\uff0c\u5728 LazyVStack \u4e0b\u6570\u636e\u5728\u6bcf\u6b21\u5237\u65b0\u540e\u624d\u4f1a\u589e\u52a0\uff0c\u5728 VStack \u4e0b\uff0c\u6570\u636e\u4f1a\u4e00\u76f4\u589e\u52a0\u3002\n        ScrollView{\n            LazyVStack{ \/\/\u6362\u6210 VStack \u4f5c\u6bd4\u8f83\n                ForEach(list,id:\\.id){ item in\n                    Text(\"id:\\(item.number)\")\n                        .onAppear {\n                            moreItem(id: item.id)\n                        }\n                }\n            }\n            if loading {\n                ProgressView()\n            }\n        }\n    }\n    \n    func moreItem(id:UUID){\n       \/\/\u5982\u679c\u662f\u6700\u540e\u4e00\u4e2a\u6570\u636e\u5219\u83b7\u53d6\u65b0\u6570\u636e\n        if id == list.last!.id &amp;&amp; loading != true {\n            loading = true\n            \/\/\u589e\u52a0\u5ef6\u65f6\uff0c\u6a21\u62df\u5f02\u6b65\u6570\u636e\u83b7\u53d6\u6548\u679c\n            DispatchQueue.main.asyncAfter(deadline: .now() + 1){\n                \/\/\u6570\u636e\u6a21\u62df\uff0c\u4e5f\u53ef\u83b7\u53d6\u7f51\u7edc\u6570\u636e\n                list.append(contentsOf: (0...30)\n                            .map{_ in Item(number:Int.random(in: 1000...5000))})\n                loading = false\n            }\n        }\n        \n    }\n}\n\n struct Item:Identifiable{\n    let id = UUID()\n    let number:Int\n}\n<\/pre>\n\n\n\n<p>LazyHStack \u7684\u7528\u6cd5\u540c LazyVStack \u4e00\u6837<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SwiftUI2.0 \u63d0\u4f9b\u4e86 LazyVStack \u548c LazyHStack\uff0c\u5176\u4f5c\u7528\u662f\u53ea\u6709\u5f53 View \u5728\u53ef\u89c1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4901"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4901"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4901\/revisions"}],"predecessor-version":[{"id":4902,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4901\/revisions\/4902"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}