{"id":4558,"date":"2021-12-14T17:47:03","date_gmt":"2021-12-14T09:47:03","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4558"},"modified":"2021-12-14T17:47:03","modified_gmt":"2021-12-14T09:47:03","slug":"%e8%ae%a9geometryreader%e6%9d%a5%e8%a7%a3%e5%86%b3%e5%90%a7","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4558","title":{"rendered":"\u8ba9GeometryReader\u6765\u89e3\u51b3\u5427"},"content":{"rendered":"\n<p>\u591a\u6570\u60c5\u51b5\u4e0b\uff0cSwiftUI \u4f1a\u505a\u65bd\u5c55\u5b83\u7684\u5e03\u5c40\u9b54\u6cd5 \uff0c\u751f\u6d3b\u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\u662f\u5982\u6b64\u7f8e\u5999 \u3002\u4e0d\u8fc7\uff0c\u4e5f\u6709\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u81ea\u5b9a\u4e49\u7684\u89c6\u56fe\u62e5\u6709\u66f4\u591a\u7684\u638c\u63a7\u3002\u5728\u8fd9\u4e9b\u65f6\u5019\uff0c\u6211\u4eec\u6709\u51e0\u79cd\u5de5\u5177 \u53ef\u4ee5\u5229\u7528\u3002\u7b2c\u4e00\u4e2a\u9700\u8981\u6211\u4eec\u53bb\u63a2\u7d22\u7684\u5c31\u662f\u00a0<strong>GeometryReader<\/strong>\u3002<\/p>\n\n\n\n<p><em><a rel=\"noreferrer noopener\" href=\"https:\/\/www.zhihu.com\/search?q=%E7%88%B6%E7%BA%A7%E8%A7%86%E5%9B%BE&amp;search_source=Entity&amp;hybrid_search_source=Entity&amp;hybrid_search_extra=%7B%22sourceType%22%3A%22article%22%2C%22sourceId%22%3A136158451%7D\" target=\"_blank\">\u7236\u7ea7\u89c6\u56fe<\/a>\u60f3\u8981\u4ec0\u4e48\uff1f<\/em><\/p>\n\n\n\n<p>\u5f53\u4f60\u7ed9\u81ea\u5b9a\u4e49\u89c6\u56fe\u7f16\u7801\u65f6\uff0c\u4f60\u901a\u5e38\u4e0d\u9700\u8981\u62c5\u5fc3\u5b83\u5468\u56f4\u7684\u73af\u5883\u548c\u5b83\u7684\u5c3a\u5bf8\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7ed8\u5236\u77e9\u5f62\u7684\u89c6\u56fe\uff0c\u4f60\u53ea\u8981\u7ed8\u5236\u4e00\u4e2a&nbsp;<code>Rectangle<\/code>&nbsp;\u5c31\u597d\u4e86\u3002\u5b83\u4f1a\u4ee5\u7236\u7ea7\u89c6\u56fe\u6307\u5b9a\u7684\u5c3a\u5bf8\u548c\u4f4d\u7f6e\u88ab\u7ed8\u5236\u3002<\/p>\n\n\n\n<p>\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5c3a\u5bf8\u4e3a 150&#215;100 \u7684\u00a0<code>VStack<\/code>\uff0c\u5b83\u9996\u5148\u653e\u7f6e\u4e86\u4e00\u4e2a\u00a0<code>Text<\/code>\u00a0\u5728\u9876\u90e8\uff0c\u7136\u540e\u5269\u4f59\u7684\u7a7a\u95f4\u88ab\u00a0<code>MyRectangle()<\/code>\u00a0\u5360\u636e\u3002\u8fd9\u4e2a\u89c6\u56fe\u662f\u5982\u6b64\u987a\u4ece\uff0c\u5b83\u51c6\u786e\u5730\u7ed8\u5236\u4e86\u6211\u4eec\u7ed9\u5b83\u7684\u84dd\u8272\u3002\u4e00\u4e2a\u50cf\u7d20\u4e0d\u591a\uff0c\u4e00\u4e2a\u50cf\u7d20\u4e0d\u5c11\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-194-1024x519.png\" alt=\"\" class=\"wp-image-4559\" width=\"509\" height=\"257\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-194-1024x519.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-194-300x152.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-194-768x389.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-194-830x421.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-194-230x117.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-194-350x177.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-194-480x243.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-194.png 1148w\" sizes=\"(max-width: 509px) 100vw, 509px\" \/><\/figure><\/div>\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 ContentView : View {\n    var body: some View {\n        \n        VStack {\n            \n            Text(\"Hello There!\")\n            MyRectangle()\n            \n        }.frame(width: 150, height: 100).border(Color.black)\n\n    }\n}\n\nstruct MyRectangle: View {\n    var body: some View {\n        Rectangle().fill(Color.blue)\n    }\n}<\/pre>\n\n\n\n<p>\u5982\u4f60\u6240\u89c1\uff0c\u89c6\u56fe<code>MyRectangle()<\/code>\u5e76\u4e0d\u5173\u5fc3\u5c3a\u5bf8\uff0c\u5b83\u53ea\u505a\u4e00\u4ef6\u4e8b\uff0c\u7ed8\u5236\u4e00\u4e2a\u77e9\u5f62\uff0c\u7531 SwiftUI \u6765\u641e\u6e05\u695a\u5b83\u7684\u7236\u7ea7\u89c6\u56fe\u9700\u8981\u5728\u54ea\u91cc\uff0c\u4ee5\u591a\u5927\u7684\u5c3a\u5bf8\u6765\u6446\u653e\u5b83\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5305\u542b\u5b83\u7684<code>VStack<\/code>\u6b63\u662f\u8fd9\u6837\u7684\u7236\u7ea7\u89c6\u56fe\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em>\u5982\u679c\u4f60\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e parents \u662f\u5982\u4f55\u51b3\u5b9a\u5b83\u4eec\u7684 children \u7684\u5c3a\u5bf8\u548c\u4f4d\u7f6e\u7684\u4fe1\u606f\uff0c\u6211\u5f3a\u70c8\u5efa\u8bae\u4f60\u89c2\u770b 2019 WWDC session 237 (<a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/developer.apple.com\/videos\/play\/wwdc2019\/237\/\" target=\"_blank\" rel=\"noreferrer noopener\">Building Custom Views with SwiftUI<\/a>).<\/em><\/p><\/blockquote>\n\n\n\n<p>\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u8fd9\u6837\u5c31\u8db3\u591f\u4e86\u3002\u4e0d\u8fc7\uff0c\u6b63\u5982 session 237 \u91cc\u63d0\u5230\u7684\uff0c\u7236\u7ea7\u89c6\u56fe\u5efa\u8bae\u5c3a\u5bf8\u548c\u4f4d\u7f6e\uff0c\u4f46\u5b50\u89c6\u56fe\u7a76\u7adf\u8981\u5982\u4f55\u4ee5\u53ca\u5728\u54ea\u7ed8\u5236\u662f\u7531\u5b83\u81ea\u5df1\u51b3\u5b9a\u7684\u3002\u5982\u679c\u5b50\u89c6\u56fe\u5bf9\u4e8e\u63d0\u4f9b\u7ed9\u5b83\u7684\u5efa\u8bae\u4e0d\u6ee1\u610f\uff0c\u5b83\u4f1a\u5ffd\u7565\u5efa\u8bae\u3002<\/p>\n\n\n\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u4f60\u7684\u81ea\u5b9a\u4e49\u89c6\u56fe\u4ee5\u7236\u7ea7\u89c6\u56fe\u5efa\u8bae\u7684\u5c3a\u5bf8\u7684\u4e00\u534a\u6765\u7ed8\u5236\u77e9\u5f62\uff0c\u5e76\u4e14\u5e0c\u671b\u5b83\u88ab\u653e\u7f6e\u5728\u8ddd\u79bb\u53f3\u8fb9\u7f18 5 \u4e2a\u70b9\u7684\u4f4d\u7f6e\uff0c\u90a3\u5c31\u8fd9\u4e48\u5e72\uff0c\u6ca1\u6709\u4eba\u80fd\u963b\u6b62\u4f60 \u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u6211\u4eec\u5728\u8fd9\u91cc\u8981\u600e\u4e48\u7f16\u5199\u4ee3\u7801\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u8fd9\u5176\u5b9e\u4e0d\u590d\u6742\uff0c<strong>\u8ba9 GeometryReader \u6765\u89e3\u51b3\u5427<\/strong>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><em>\u5b50\u89c6\u56fe\u505a\u4e86\u4ec0\u4e48\uff1f<\/em><\/h4>\n\n\n\n<p>\u8ba9\u6211\u4eec\u770b\u4e00\u4e0bApple \u63d0\u4f9b\u7684\u5173\u4e8e&nbsp;<a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/developer.apple.com\/documentation\/swiftui\/geometryreader\" target=\"_blank\" rel=\"noreferrer noopener\">GeometryReader<\/a>&nbsp;\u7684\u6587\u6863\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em>A container view that defines its content as a function of its own size and coordinate space.<\/em><\/p><\/blockquote>\n\n\n\n<p>\u76f8\u5bf9\u4e8e\u5176\u4ed6\u51e0\u4e4e\u90fd\u662f \u201cNo overview available\u201d \u7684\u6587\u6863\uff0c\u8fd9\u4e2a\u89e3\u91ca\u5df2\u7ecf\u7b97\u5f88\u8be6\u7ec6\u4e86\u3002<\/p>\n\n\n\n<p>\u90a3\u8fd9\u53e5\u8bdd\u662f\u4ec0\u4e48\u610f\u601d\u5462\uff1f\u7b80\u5355\u6765\u8bb2\uff0cGeometryReader \u5c31\u662f\u53e6\u5916\u4e00\u79cd\u89c6\u56fe\u3002\u60ca\u4e0d\u60ca\u559c\uff1f\u5728 SwiftUI \u4e2d\uff0c\u51e0\u4e4e<strong>\u6240\u6709\u4e1c\u897f<\/strong>\u90fd\u662f\u89c6\u56fe\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cGeometryReader \u8ba9\u4f60\u5b9a\u4e49\u4e86\u5b83\u7684\u5185\u5bb9\u3002 \u4f46\u662f\u4e0e\u5176\u4ed6\u89c6\u56fe\u4e0d\u540c\u3002\u4f60\u53ef\u4ee5\u62ff\u5230\u4e00\u4e9b\u4f60\u5728\u5176\u4ed6\u89c6\u56fe\u4e2d\u62ff\u4e0d\u5230\u7684\u4fe1\u606f\uff0c\u5e76\u501f\u52a9\u5b83\u4eec\u505a\u4e00\u4e9b\u4f60\u81ea\u5df1\u7684\u4e8b\u60c5\u3002<\/p>\n\n\n\n<p>\u4e0a\u9762\u8bf4\u5230\u6211\u4eec\u8981\u7ed8\u5236\u4e00\u4e2a\u5927\u5c0f\u662f\u7236\u7ea7\u89c6\u56fe\u5efa\u8bae\u5927\u5c0f\u4e00\u534a\u7684\u89c6\u56fe\uff0c\u5e76\u4e14\u6446\u653e\u5728\u8ddd\u79bb\u5efa\u8bae\u533a\u57df\u53f3\u8fb9\u7f18 5 \u4e2a\u70b9\u7684\u5730\u65b9\u3002\u6709\u4e86 GeometryReader\uff0c\u8fd9\u5c31\u5f88\u7b80\u5355\u4e86\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-195-1024x542.png\" alt=\"\" class=\"wp-image-4560\" width=\"538\" height=\"284\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-195-1024x542.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-195-300x159.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-195-768x406.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-195-830x439.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-195-230x122.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-195-350x185.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-195-480x254.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-195.png 1108w\" sizes=\"(max-width: 538px) 100vw, 538px\" \/><\/figure><\/div>\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 ContentView : View {\n    var body: some View {\n        \n        VStack {\n            \n            Text(\"Hello There!\")\n            MyRectangle()\n            \n        }.frame(width: 150, height: 100).border(Color.black)\n\n    }\n}\n\nstruct MyRectangle: View {\n    var body: some View {\n        GeometryReader { geometry in\n            Rectangle()\n                .path(in: CGRect(x: geometry.size.width + 5,\n                                 y: 0,\n                                 width: geometry.size.width \/ 2.0,\n                                 height: geometry.size.height \/ 2.0))\n                .fill(Color.blue)\n            \n        }\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><em>GeometryProxy<\/em><\/h4>\n\n\n\n<p>\u4f60\u4ed4\u7ec6\u770b\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u4f1a\u53d1\u73b0\u4e00\u4e2a\u5305\u542b geometry \u53d8\u91cf\u7684\u95ed\u5305\uff0c\u8fd9\u53d8\u91cf\u662f\u4e00\u4e2a&nbsp;<strong>GeometryProxy<\/strong>&nbsp;\u7c7b\u578b\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7&nbsp;<a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/swiftui-lab.com\/communicating-with-the-view-tree-part-1\/\" target=\"_blank\" rel=\"noreferrer noopener\">Inspecting the View Tree (\u68c0\u89c6\u89c6\u56fe\u6811)<\/a>&nbsp;\u8fd9\u7bc7\u6587\u7ae0\u53bb\u4e86\u89e3\u66f4\u591a\u76f8\u5173\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u5728 GeometryProxy \u7c7b\u4e2d\u6709\u4e24\u4e2a\u8ba1\u7b97\u578b\u5c5e\u6027\uff0c\u4e00\u4e2a\u65b9\u6cd5\uff0c\u548c\u4e00\u4e2a\u4e0b\u6807 getter\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=\"\">public var size: CGSize { get }\npublic var safeAreaInsets: EdgeInsets { get }\npublic func frame(in coordinateSpace: CoordinateSpace) -> CGRect\npublic subscript&lt;T>(anchor: Anchor&lt;T>) -> T where T : Equatable { get }<\/pre>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u5c5e\u6027\u5f88\u76f4\u767d\uff0c\u6b63\u5982\u5728\u4f8b\u5b50\u4e2d\u770b\u5230\u7684\uff0c<strong>size<\/strong>\u5c5e\u6027\u662f\u7236\u7ea7\u89c6\u56fe\u5efa\u8bae\u7684\u5927\u5c0f\u3002\u6211\u4eec\u53ef\u4ee5\u5151\u73b0\u5b83\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u8fd9\u4e48\u3002<strong><em>\u8bb0\u4f4f\uff0c\u8fd9\u53d6\u51b3\u4e8e\u5b50\u89c6\u56fe\u3002<\/em><\/strong><\/p>\n\n\n\n<p>GeometryProxy \u628a<strong>safeAreaInsets<\/strong>\u4e5f\u66b4\u9732\u7ed9\u4e86\u6211\u4eec\u3002<\/p>\n\n\n\n<p><strong>frame<\/strong>\u65b9\u6cd5\u66b4\u9732\u7ed9\u6211\u4eec\u7236\u7ea7\u89c6\u56fe\u5efa\u8bae\u533a\u57df\u7684\u77e9\u5f62\uff0c\u53ef\u4ee5\u501f\u52a9\u4f20\u5165<strong>.local, .global, .named()<\/strong>\u6765\u83b7\u53d6\u4e0d\u540c\u7684\u5750\u6807\u7a7a\u95f4\u3002\u672c\u5730\u548c<a href=\"https:\/\/www.zhihu.com\/search?q=%E5%85%A8%E5%B1%80%E5%9D%90%E6%A0%87%E7%A9%BA%E9%97%B4&amp;search_source=Entity&amp;hybrid_search_source=Entity&amp;hybrid_search_extra=%7B%22sourceType%22%3A%22article%22%2C%22sourceId%22%3A136158451%7D\" target=\"_blank\" rel=\"noreferrer noopener\">\u5168\u5c40\u5750\u6807\u7a7a\u95f4<\/a>\u4e0d\u7528\u591a\u89e3\u91ca\uff0c<code>.named()<\/code>\u7528\u6765\u83b7\u53d6\u4e00\u4e2a\u88ab\u547d\u540d\u7684<a href=\"https:\/\/www.zhihu.com\/search?q=%E5%9D%90%E6%A0%87%E7%A9%BA%E9%97%B4&amp;search_source=Entity&amp;hybrid_search_source=Entity&amp;hybrid_search_extra=%7B%22sourceType%22%3A%22article%22%2C%22sourceId%22%3A136158451%7D\" target=\"_blank\" rel=\"noreferrer noopener\">\u5750\u6807\u7a7a\u95f4<\/a>\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u540d\u79f0\u6765\u83b7\u53d6\u5176\u4ed6\u89c6\u56fe\u7684\u5750\u6807\u7a7a\u95f4\u3002<a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/swiftui-lab.com\/communicating-with-the-view-tree-part-1\/\" target=\"_blank\" rel=\"noreferrer noopener\">Inspecting the View Tree<\/a>&nbsp;\u8fd9\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4e86\u5177\u4f53\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<strong>\u4e0b\u6807\u53d6\u503c<\/strong>\u6765\u83b7\u53d6\u4e00\u4e2a<strong>\u951a\u70b9<\/strong>\u3002\u6fc0\u8d77\u4f60\u7684\u597d\u5947\u5fc3\u4e86\u5bf9\u4e0d\u5bf9\uff1f \u8fd9\u4e2a\u662f GeometryReader \u7684\u4e00\u4e2a\u76f8\u5f53\u70ab\u9177\u7684\u529f\u80fd\uff0c\u540c\u65f6\u8fd9\u4e5f\u662f\u4e00\u4e2a\u7b28\u91cd\u7684\u4e3b\u9898\uff0c\u6211\u5728<a rel=\"noreferrer noopener\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/swiftui-lab.com\/communicating-with-the-view-tree-part-2\/\" target=\"_blank\">Inspecting the View Tree<\/a>\u7684\u7b2c\u4e8c\u90e8\u5206\u6709\u8bb2\u89e3\u3002\u76ee\u524d\uff0c\u4f60\u9700\u8981\u77e5\u9053\u7684\u662f\uff0c\u501f\u52a9\u4e0b\u6807\uff0c\u4f60\u53ef\u4ee5\u83b7\u53d6\u89c6\u56fe\u6811\u4e2d\u4efb\u4f55\u5b50\u89c6\u56fe\u7684\u5c3a\u5bf8\u548c\u4f4d\u7f6e\u3002\u662f\u4e0d\u662f\u5f88\u5f3a\u5927\uff1f\u8bb0\u5f97\u56de\u6765\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u5b83\u7684\u77e5\u8bc6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><em>\u5438\u6536\u53e6\u4e00\u4e2a\u89c6\u56fe\u7684 Geometry<\/em><\/h4>\n\n\n\n<p>GeometryReader \u529f\u80fd\u5df2\u7ecf\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u5982\u679c\u5b83\u88ab\u7ed3\u5408&nbsp;<strong>.background()<\/strong>&nbsp;\u6216\u8005&nbsp;<strong>.overlay()<\/strong>&nbsp;modifier \u6765\u4f7f\u7528\uff0c\u529f\u80fd\u5c31\u4f1a\u66f4\u52a0\u5f3a\u5927\u3002<\/p>\n\n\n\n<p>\u5728\u6211\u89c1\u8fc7\u7684\u5927\u591a\u6570\u6559\u7a0b\u91cc\uff0cbackground \u90fd\u662f\u4ee5\u6700\u7b80\u5355\u65b9\u5f0f\u4f7f\u7528\u7684\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a<code>Text(\"hello\").background(Color.red)<\/code>\u3002\u7b2c\u4e00\u773c\u770b\uff0c\u6211\u4eec\u4f1a\u4ee5\u4e3a<code><a href=\"https:\/\/www.zhihu.com\/search?q=Color.red&amp;search_source=Entity&amp;hybrid_search_source=Entity&amp;hybrid_search_extra=%7B%22sourceType%22%3A%22article%22%2C%22sourceId%22%3A136158451%7D\" target=\"_blank\" rel=\"noreferrer noopener\">Color.red<\/a><\/code>\u662f\u4e00\u4e2a\u989c\u8272\u53c2\u6570\uff0c\u5176\u5b9e\u5b83\u5e76\u4e0d\u662f\uff0c<code>Color.red<\/code>\u662f\u53c8\u4e00\u4e2a\u89c6\u56fe\uff01\u5b83\u552f\u4e00\u7684\u529f\u80fd\u5c31\u662f\u628a\u7236\u7ea7\u89c6\u56fe\u5efa\u8bae\u7ed9\u5b83\u7684\u533a\u57df\u586b\u5145\u6210\u7ea2\u8272\u3002\u56e0\u4e3a\u7236\u7ea7\u89c6\u56fe\u662f\u80cc\u666f\uff0c\u800c\u80cc\u666f\u4fee\u6539\u4e86<code>Text<\/code>\uff0c\u6240\u4ee5\u5efa\u8bae<code>Color.red<\/code>\u586b\u5145\u7684\u533a\u57df\u5c31\u662f<code>Text(\"hello\")<\/code>\u6240\u5360\u636e\u7684\u533a\u57df\u3002\u662f\u4e0d\u662f\u5f88\u4f18\u96c5\uff1f<\/p>\n\n\n\n<p><strong>.overlay<\/strong>&nbsp;modifier \u7684\u9053\u7406\u4e00\u6837\uff0c\u53ea\u4e0d\u8fc7\u5b83\u4e0d\u662f\u7ed9\u88ab\u4fee\u6539\u7684\u89c6\u56fe\u7ed8\u5236\u80cc\u666f\uff0c\u800c\u662f\u53e0\u5728\u524d\u9762\u7ed8\u5236\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u80fd\u610f\u8bc6\u5230\u4f60\u53ef\u4ee5\u628a\u4efb\u610f\u89c6\u56fe\u585e\u8fdb\u00a0<code>.background()<\/code>\uff0c\u4e0d\u5355\u662f\u00a0<code>Color()<\/code>\u3002\u4e3a\u4e86\u6f14\u793a\u6211\u4eec\u53ef\u4ee5\u5982\u4f55\u5229\u7528\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u5c06\u7ed3\u5408 GeometryReader\uff0c\u753b\u4e00\u4e2a\u77e9\u5f62\u80cc\u666f\uff0c\u540c\u65f6\u7ed9\u77e9\u5f62\u7684\u89d2\u6307\u5b9a\u4e0d\u540c\u7684\u5706\u89d2\u534a\u5f84\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-196-1024x525.png\" alt=\"\" class=\"wp-image-4562\" width=\"518\" height=\"265\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-196-1024x525.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-196-300x154.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-196-768x394.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-196-830x425.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-196-230x118.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-196-350x179.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-196-480x246.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-196.png 1120w\" sizes=\"(max-width: 518px) 100vw, 518px\" \/><\/figure><\/div>\n\n\n\n<p>\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\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 ContentView : View {\n    var body: some View {\n        \n        HStack {\n            \n            Text(\"SwiftUI\")\n                .foregroundColor(.black).font(.title).padding(15)\n                .background(RoundedCorners(color: .green, tr: 30, bl: 30))\n            \n            Text(\"Lab\")\n                .foregroundColor(.black).font(.title).padding(15)\n                .background(RoundedCorners(color: .blue, tl: 30, br: 30))\n            \n        }.padding(20).border(Color.gray).shadow(radius: 3)\n    }\n}\n\nstruct RoundedCorners: View {\n    var color: Color = .black\n    var tl: CGFloat = 0.0\n    var tr: CGFloat = 0.0\n    var bl: CGFloat = 0.0\n    var br: CGFloat = 0.0\n    \n    var body: some View {\n        GeometryReader { geometry in\n            Path { path in\n                \n                let w = geometry.size.width\n                let h = geometry.size.height\n                \n                \/\/ \u786e\u4fdd\u5706\u89d2\u534a\u5f84\u4e0d\u4f1a\u8d85\u51fa\u754c\u9650\n                let tr = min(min(self.tr, h\/2), w\/2)\n                let tl = min(min(self.tl, h\/2), w\/2)\n                let bl = min(min(self.bl, h\/2), w\/2)\n                let br = min(min(self.br, h\/2), w\/2)\n                \n                path.move(to: CGPoint(x: w \/ 2.0, y: 0))\n                path.addLine(to: CGPoint(x: w - tr, y: 0))\n                path.addArc(center: CGPoint(x: w - tr, y: tr), radius: tr, startAngle: Angle(degrees: -90), endAngle: Angle(degrees: 0), clockwise: false)\n                path.addLine(to: CGPoint(x: w, y: h - br))\n                path.addArc(center: CGPoint(x: w - br, y: h - br), radius: br, startAngle: Angle(degrees: 0), endAngle: Angle(degrees: 90), clockwise: false)\n                path.addLine(to: CGPoint(x: bl, y: h))\n                path.addArc(center: CGPoint(x: bl, y: h - bl), radius: bl, startAngle: Angle(degrees: 90), endAngle: Angle(degrees: 180), clockwise: false)\n                path.addLine(to: CGPoint(x: 0, y: tl))\n                path.addArc(center: CGPoint(x: tl, y: tl), radius: tl, startAngle: Angle(degrees: 180), endAngle: Angle(degrees: 270), clockwise: false)\n                }\n                .fill(self.color)\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>\u53e6\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5e94\u7528\u76f8\u540c\u7684\u89c6\u56fe\uff0c\u4f46\u662f\u91c7\u7528 0.5 \u7684\u900f\u660e\u5ea6\uff0c\u5e94\u7528\u5728\u6587\u672c\u4e0a\u9762\uff0c\u7528 .<code>overlay()<\/code>\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-197-1024x511.png\" alt=\"\" class=\"wp-image-4563\" width=\"510\" height=\"254\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-197-1024x511.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-197-300x150.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-197-768x383.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-197-830x414.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-197-230x115.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-197-350x175.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-197-480x240.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-197.png 1110w\" sizes=\"(max-width: 510px) 100vw, 510px\" \/><\/figure><\/div>\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(\"SwiftUI\")\n    .foregroundColor(.black).font(.title).padding(15)\n    .overlay(RoundedCorners(color: .green, tr: 30, bl: 30).opacity(0.5))\nText(\"Lab\")\n    .foregroundColor(.black).font(.title).padding(15)\n    .overlay(RoundedCorners(color: .blue, tl: 30, br: 30).opacity(0.5))<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u9e21\u4e0e\u86cb\u7684\u95ee\u9898<\/h4>\n\n\n\n<p>\u5f53\u4f60\u5f00\u59cb\u4f7f\u7528 GeometryReader\uff0c\u4f60\u5f88\u5feb\u4f1a\u53d1\u73b0\u6240\u8c13\u7684\u9e21\u4e0e\u86cb\u7684\u95ee\u9898\u3002\u56e0\u4e3a GeometryReader \u9700\u8981\u63d0\u4f9b\u6240\u6709\u7684\u53ef\u7528\u7a7a\u95f4\u7ed9\u5b50\u89c6\u56fe\uff0c\u5b83\u9996\u5148\u9700\u8981\u5c3d\u53ef\u80fd\u591a\u5730\u5360\u636e\u7a7a\u95f4\u3002\u7136\u540e\uff0c\u5982\u6211\u4eec\u77e5\u9053\u7684\uff0c\u5b50\u89c6\u56fe\u53ef\u80fd\u51b3\u5b9a\u4f7f\u7528\u66f4\u5c11\u7684\u7a7a\u95f4\u3002\u7ed3\u679c\uff0cGeometryReader \u8fd8\u662f\u5c3d\u53ef\u80fd\u5730\u4fdd\u6301\u5927\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u80fd\u4f1a\u60f3\uff0c\u4e00\u65e6\u5b50\u89c6\u56fe\u786e\u5b9a\u4e86\u9700\u8981\u7684\u7a7a\u95f4\uff0c\u53ef\u4ee5\u5f3a\u5236 GeometryReader \u76f8\u5e94\u5730\u7f29\u5c0f\u3002\u4f46\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u5b50\u89c6\u56fe\u6839\u636e GeometryReader \u91cd\u65b0\u8ba1\u7b97\u65b0\u7684\u5c3a\u5bf8\u2026 \u4e00\u4e2a\u5faa\u73af\u5c31\u4ea7\u751f\u4e86\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u7a76\u7adf\u662f\u54ea\u4e2a\u5728\u5148\u5462\uff1f\u662f\u5b50\u7ea7\u4f9d\u8d56\u7236\u7ea7\u7684\u5c3a\u5bf8\uff0c\u8fd8\u662f\u7236\u7ea7\u4f9d\u8d56\u5b50\u7ea7\u7684\u5c3a\u5bf8\uff1f \u4f46\u4f60\u9762\u5bf9\u8fd9\u79cd\u95ee\u9898\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u9000\u56de\u53bb\u91cd\u65b0\u601d\u8003\u4f60\u7684\u65b9\u6848\uff0c\u521b\u9020\u529b\u662f\u8131\u79bb\u8fd9\u79cd\u56f0\u5883\u7684\u5173\u952e\u3002\u4e0d\u8fc7\u8c01\u53c8\u77e5\u9053\u5462\uff0c\u4e5f\u8bb8 GeometryReader \u5728\u8fd9\u91cc\u5e76\u4e0d\u9002\u7528\u4e8e\u89e3\u51b3\u8fd9\u79cd\u5e03\u5c40\u95ee\u9898\u3002\u5e78\u8fd0\u7684\u662f\uff0cGeometryReader \u5e76\u4e0d\u662f\u552f\u4e00\u7684\u5e03\u5c40\u5de5\u5177\u3002\u4f60\u53ef\u4ee5\u4ece\u6211\u7684\u4e0b\u4e00\u7bc7\u6587\u7ae0\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/swiftui-lab.com\/communicating-with-the-view-tree-part-1\/\" target=\"_blank\">Preferences and Anchor Preferences<\/a>\u00a0\u91cc\u627e\u5230\u4f8b\u5b50\u3002&#8217;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u603b\u7ed3<\/h4>\n\n\n\n<p>\u4eca\u5929\u6211\u4eec\u4e86\u89e3\u5230\u6211\u4eec\u53ef\u4ee5\u5229\u7528 GeometryReader \u6765\u6784\u5efa\u53ef\u611f\u77e5\u81ea\u8eab\u5728\u5c4f\u5e55\u4e0a\u7684\u5c3a\u5bf8\u548c\u4f4d\u7f6e\u7684\u81ea\u5b9a\u4e49\u89c6\u56fe\u3002\u6211\u4eec\u8fd8\u4e86\u89e3\u5230\u4e00\u4e9b\u5173\u4e8e\u4ece\u5176\u4ed6\u89c6\u56fe\u201c\u7a83\u53d6\u201d\u51e0\u4f55\u4fe1\u606f\uff0c\u4e3a\u6211\u4eec\u6240\u7528\u7684\u6709\u7528\u63d0\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u6570\u60c5\u51b5\u4e0b\uff0cSwiftUI \u4f1a\u505a\u65bd\u5c55\u5b83\u7684\u5e03\u5c40\u9b54\u6cd5 \uff0c\u751f\u6d3b\u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\u662f\u5982\u6b64\u7f8e\u5999 \u3002\u4e0d\u8fc7\uff0c\u4e5f\u6709\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4558"}],"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=4558"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4558\/revisions"}],"predecessor-version":[{"id":4565,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4558\/revisions\/4565"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}