{"id":903,"date":"2020-08-19T13:24:36","date_gmt":"2020-08-19T05:24:36","guid":{"rendered":"http:\/\/123.57.164.21\/?p=903"},"modified":"2020-08-19T13:24:36","modified_gmt":"2020-08-19T05:24:36","slug":"swiftui-viewbuilder","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=903","title":{"rendered":"SwiftUI @ViewBuilder"},"content":{"rendered":"\n<p><strong>\u4ec0\u4e48\u662f@ViewBuilder?<\/strong><\/p>\n\n\n\n<p>\u4ece\u5b57\u9762\u610f\u601d\u53bb\u7406\u89e3 ViewBuilder \u5c31\u662f<code>\u89c6\u56fe\u6784\u5efa<\/code>\uff0c\u5176\u4e3b\u8981\u4f7f\u7528\u573a\u666f\u5c31\u662f\u6784\u5efa\u89c6\u56fe\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7ViewBuilder\u628a\u8bb8\u591a\u5177\u4f53\u76f8\u540c\u7279\u70b9\u7684View\u5c01\u88c5\u8d77\u6765\uff0c\u5e76\u4e14\u5206\u79bb\u903b\u8f91\u4ee3\u7801\u548c\u89c6\u56fe\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\uff0c\u5e76\u589e\u5f3a\u53ef\u8bfb\u6027\u3002<\/p>\n\n\n\n<p>\u770b\u4e86\u4e0b\u9762\u7684\u4f8b\u5b50\u4f60\u53ef\u80fd\u5c31\u4f1a\u5bf9@ViewBuilder\u7684\u4f7f\u7528\u6709\u4e00\u4e2a\u66f4\u597d\u7684\u7406\u89e3\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u60f3\u8981\u8bbe\u7f6e\u4e00\u4e2aText\u7684\u80cc\u666f\u4e3a\u7ea2\u8272\uff0c\u5706\u89d2\u4e3a5\u3002\u6211\u4eec\u53ef\u80fd\u8fd9\u6837\u53bb\u5199:<\/p>\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=\"\">Text(\"Liaoworking\")\n.background(Color.red)\n.cornerRadius(5)<\/pre>\n\n\n\n<p>\u540e\u6765\u4ea7\u54c1\u52a0\u9700\u6c42\u4e86\uff1a \u56fe\u7247\u4e5f\u662f\u7ea2\u8272\u7684\u80cc\u666f\uff0c\u5706\u89d2\u4e3a5\uff1a<\/p>\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=\"\">Image(\"Liaoworking\")\n.background(Color.red)\n.cornerRadius(5)<\/pre>\n\n\n\n<p>\u518d\u540e\u6765\u4ea7\u54c1\u53c8\u52a0\u9700\u6c42\uff1a\u4e00\u4e9b\u81ea\u5b9a\u4e49\u89c6\u56fe\u4e5f\u662f\u7ea2\u8272\u7684\u80cc\u666f\uff0c\u5706\u89d2\u4e3a5\uff1a<br>\u8fd9\u91cc\u5047\u8bbe\u6211\u4eec\u7684\u81ea\u5b9a\u4e49\u89c6\u56fe\u7c7b\u4e3aMyView<\/p>\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=\"\">MyView()\n.background(Color.red)\n.cornerRadius(5)\n<\/pre>\n\n\n\n<p>\u806a\u660e\u7684\u4f60\u80af\u5b9a\u60f3\u5230\u4e86\u7528\u4e00\u4e2aView\u7684extension\u6765\u7edf\u4e00\u5904\u7406\u3002<\/p>\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=\"\">extension View {\n    func addRedBGWithRoundCorner() -> some View {\n        self\n        .background(Color.red)\n        .cornerRadius(5)\n    }\n}\n\n\/\/\u8c03\u7528\uff1a\nText(\"Liaoworking\").addRedBGWithRoundCorner()\n<\/pre>\n\n\n\n<p>\u8fd9\u6837\u505a\u7684\u786e\u53ef\u4ee5\u8fbe\u5230\u76f8\u540c\u7684\u6548\u679c\uff0c\u800c\u4e14\u4ee3\u7801\u4e5f\u4f1a\u7b80\u6d01\u4e0d\u5c11\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u65f6\u5019\u4f60\u8fd8\u53ef\u4ee5\u7528<code>@ViewBuilder<\/code>\u6ce8\u89e3\u6765\u521b\u5efa\u4f60\u7684\u81ea\u5b9a\u4e49\u89c6\u56fe\u8fbe\u5230\u76f8\u540c\u7684\u6548\u679c\uff1a<\/p>\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 RedBackgroundAndCornerView&lt;Content: View>: View {\n    let content: Content\n    \n    init(@ViewBuilder content: () -> Content) {\n        self.content = content()\n    }\n    \n    var body: some View {\n        content\n            .background(Color.red)\n            .cornerRadius(5)\n    }\n}\n\n\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a\n\nRedBackgroundAndCornerView {\n    Text(\"liaoworking\")\n}<\/pre>\n\n\n\n<p>\u4e24\u79cd\u5c01\u88c5\u7684\u6700\u540e\u6548\u679c\u90fd\u662f\u4e00\u6837\u7684\uff0c\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\uff0c<code>View\u7684extension\u5c01\u88c5\u8981\u6bd4@ViewBuilder \u5c01\u88c5 \u597d\u592a\u591a<\/code>\u3002\u4ee3\u7801\u90fd\u5c11\u5199\u5f88\u591a\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u7a81\u7136\u6709\u4e00\u5929\uff0c\u4f60\u7684\u4ea7\u54c1\u7ecf\u7406\u51b3\u5b9a\u518d\u52a0\u4e00\u4e2a\u529f\u80fd\uff0c\u4e0a\u9762\u7684\u8fd9\u4e9b\u89c6\u56fe\u70b9\u51fb\u540e\u90fd\u81ea\u52a8\u9690\u85cf\uff0c\u4f60\u4f1a\u600e\u4e48\u5199\uff1f<\/p>\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=\"\">@State var needHideText: Bool = false\n\nText(\"Liaoworking\")\n    .addRedBGWithRoundCorner()\n    .opacity(needHideText ? 0.0 : 1.0)\n    .onTapGesture {\n        self.needHideText = true\n    }<\/pre>\n\n\n\n<p>\u7136\u540e\u5230\u4e86Image\uff0c\u8fd8\u6709MyView\uff0c\u00a0<code>\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u9700\u8981\u9690\u85cf\u7684\u5bf9\u8c61\uff0c\u4f60\u90fd\u5f97\u521b\u5efa\u4e00\u4e2a\u7c7b\u4f3c\u4e8eneedHideText\u7684\u53d8\u91cf\u6765\u63a7\u5236\u663e\u793a\u9690\u85cf\u903b\u8f91<\/code>\u3002 \u5230\u6700\u540e\u4f60\u7684\u91cd\u590d\u4ee3\u7801\u53ef\u80fd\u4f1a\u8d8a\u6765\u8d8a\u591a\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3aextension<code>\u65e0\u6cd5\u53bb\u5b58\u50a8\u63a7\u5236\u9690\u85cf\u903b\u8f91\u7684\u53d8\u91cf<\/code>\uff0c\u8fd9\u4e2a\u65f6\u5019@ViewBuilder\u7684\u5148\u5929\u4f18\u52bf\u9a6c\u4e0a\u5c31\u4f53\u73b0\u51fa\u6765\u4e86\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ea\u9700\u8981\u5c06\u903b\u8f91\u4ee3\u7801\u5728@ViewBuilder\u4e2d\u5199\u4e00\u6b21\uff0c\u6240\u6709\u7684View\u5c31\u5177\u6709\u4e86\u76f8\u540c\u7684\u7279\u6027\u3002<\/p>\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 RedBackgroundAndCornerView&lt;Content: View>: View {\n    let content: Content\n    @State var needHidden: Bool = false\n    \n    init(@ViewBuilder content: () -> Content) {\n        self.content = content()\n    }\n    \n    var body: some View {\n        content\n            .background(Color.red)\n            .cornerRadius(5)\n            .opacity(needHidden ? 0.0 : 1.0)\n            .onTapGesture {\n                self.needHidden = true\n        }\n    }\n}\n\n\/\/ \u6240\u6709\u88abRedBackgroundAndCornerView\u5305\u88f9\u7684View\u5c31\u90fd\u5177\u6709\u4e86\u70b9\u51fb\u540e\u9690\u85cf\u7684\u529f\u80fd\u4e86\u3002\n\nRedBackgroundAndCornerView {\n    Text(\"liaoworking\")\n}\n\nRedBackgroundAndCornerView {\n    Image(\"liaoworking\")\n}\n\nRedBackgroundAndCornerView {\n    MyView(\"liaoworking\")\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<p>@ViewBuilder\u662f\u4e00\u4e2a\u5c01\u88c5\u53ef\u590d\u7528view\u903b\u8f91\u7684\u5229\u5668\u3002\u5b83\u6700\u5927\u7684\u597d\u5904\u5c31\u662f\u628a\u4f60<code>\u903b\u8f91\u4ee3\u7801\u548c\u4f60\u7684\u89c6\u56fe\u5265\u79bb\u5f00<\/code>\u3002\u8ba9\u4ee3\u7801\u7684<code>\u53ef\u7ef4\u62a4\u6027\u548c\u6613\u8bfb\u6027\u6709\u5f88\u5927\u63d0\u5347<\/code>\u3002\u6211\u5728\u4e4b\u524d\u7684\u9879\u76ee\u91cc\u4e00\u5f00\u59cb\u5199\u8fc7\u5f88\u591a\u5783\u573e\u4ee3\u7801\uff0c\u540e\u6765\u77e5\u9053\u4e86@ViewBuilder\uff0c\u8fd9\u7684\u786e\u5728\u5bf9\u76f8\u540c\u903b\u8f91View\u7684\u5c01\u88c5\u548c\u4f7f\u7528\u4e0a\u6709\u4e86\u5f88\u5927\u7684\u4fbf\u6377\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f@ViewBuilder? \u4ece\u5b57\u9762\u610f\u601d\u53bb\u7406\u89e3 ViewBuilder \u5c31\u662f\u89c6\u56fe\u6784\u5efa\uff0c\u5176\u4e3b\u8981\u4f7f\u7528\u573a\u666f\u5c31\u662f\u6784 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/903"}],"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=903"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/903\/revisions"}],"predecessor-version":[{"id":905,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/903\/revisions\/905"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}