{"id":786,"date":"2020-08-15T21:16:43","date_gmt":"2020-08-15T13:16:43","guid":{"rendered":"http:\/\/123.57.164.21\/?p=786"},"modified":"2020-08-15T21:21:49","modified_gmt":"2020-08-15T13:21:49","slug":"swiftui-%e5%ae%89%e5%85%a8%e6%9b%b4%e6%96%b0view","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=786","title":{"rendered":"SwiftUI \u5b89\u5168\u66f4\u65b0View"},"content":{"rendered":"\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u89e3\u5728SwiftUI\u4e2d\u5982\u4f55\u5b89\u5168\u7684\u66f4\u65b0View\uff0c\u80fd\u591f\u8ba9\u5927\u5bb6\u660e\u767dSwiftUI\u4e2dView\u7684\u5237\u65b0\u76f8\u5173\u7684\u539f\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">1. <strong>View\u7684State\u662f\u4ec0\u4e48\uff1f<\/strong><\/h4>\n\n\n\n<p>View\u72b6\u6001\u7684\u5b9a\u4e49\u5e76\u6ca1\u6709\u4e00\u4e2a\u6807\u51c6\u7684\u7b54\u6848\uff0c\u6211\u4eec\u6682\u65f6\u628a\u5b83\u5b9a\u4e49\u4e3a\uff1a<strong>\u5728\u67d0\u4e00\u65f6\u523b\uff0cView\u4e2d\u6240\u6709\u7528@State\u4fee\u9970\u7684\u53d8\u91cf\u7684\u77ac\u65f6\u503c\u3002<\/strong>\u6211\u7528\u77ac\u65f6\u503c\u8fd9\u4e00\u8bf4\u6cd5\uff0c\u53ea\u662f\u60f3\u8868\u8fbe\u90a3\u4e00\u65f6\u523b\u7684\u503c\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 ContentView: View {\n    @State var show = false\n    var body: some View {\n        Example4()\n    }\n}<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u51fa\uff0cbody\u662f\u4e00\u4e2a\u8ba1\u7b97\u5c5e\u6027\uff0c\u5f53\u6211\u4eec\u9700\u8981\u5728body\u4e2d\u66f4\u65b0show\u65f6\uff0c\u5c31\u6709\u53ef\u80fd\u4f1a\u53d1\u751f\u672a\u77e5\u7684\u540e\u679c\uff0c\u8fd9\u4e2a\u6211\u4eec\u5728\u4e0b\u8fb9\u8be6\u7ec6\u8bb2\u89e3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong>Updating the State View<\/strong><\/h4>\n\n\n\n<p>\u5148\u7ed9\u5927\u5bb6\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\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 MyView: View {\n    @State private var flag = false\n    \n    var body: some View {\n        Button(\"Toggle Flag\") {\n            self.flag.toggle()\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u5927\u5bb6\u5bf9\u8fd9\u6bb5\u4ee3\u7801\u592a\u719f\u6089\u4e86\uff0c\u6211\u4eec\u77e5\u9053view\u5728\u8ba1\u7b97body\u7684\u65f6\u5019\uff0c\u4e0d\u80fd\u4fee\u6539view\u4e2d\u7684\u72b6\u6001\uff0c\u90a3\u4e48\u8fd9\u79cd\u5199\u6cd5\u4e3a\u4ec0\u4e48\u6ca1\u95ee\u9898\u5462\uff1f<\/p>\n\n\n\n<p>\u7b54\u6848\u975e\u5e38\u7b80\u5355\uff0c\u4fee\u6539\u72b6\u6001\u7684\u4ee3\u7801<code>self.flag.toggle()<\/code>\u5728\u4e00\u4e2a\u95ed\u5305\u4e2d\uff0c\u5f53\u8ba1\u7b97body\u7684\u65f6\u5019\uff0c\u5e76\u4e0d\u4f1a\u6267\u884c\u8be5\u95ed\u5305\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u8ba1\u7b97body\u7684\u65f6\u5019\uff0c\u5e76\u6ca1\u6709\u4fee\u6539\u72b6\u6001\uff0c\u53ea\u6709\u70b9\u51fb\u4e86\u6309\u94ae\u540e\uff0cview\u7684\u72b6\u6001\u624d\u88ab\u4fee\u6539\uff0c\u518d\u6b21\u89e6\u53d1body\u7684\u8ba1\u7b97\u3002<\/p>\n\n\n\n<p>\u4e00\u65e6\u6211\u4eec\u4fee\u6539\u72b6\u6001\u7684\u65b9\u5f0f\u6539\u53d8\u4e86\uff0c\u5c31\u4f1a\u4ea7\u751f\u95ee\u9898\uff0c\u770b\u4e0b\u8fb9\u7684\u4ee3\u7801\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 OutOfControlView: View {\n    @State private var count: Int = 0\n\n    var body: some View {\n        self.count += 1\n\n        return Text(\"\u8ba1\u7b97\u6b21\u6570\uff1a\\(self.count)\")\n            .multilineTextAlignment(.center)\n    }\n}<\/pre>\n\n\n\n<p>\u8fd0\u884c\u7a0b\u5e8f\u540e\uff0c\u6211\u4eec\u4f1a\u5f97\u5230\u4e00\u4e2a\u8fd0\u884c\u65f6\u7684\u63d0\u793a\u4fe1\u606f\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"68\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-88-1024x68.png\" alt=\"\" class=\"wp-image-787\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-88-1024x68.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-88-300x20.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-88-768x51.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-88-830x55.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-88-230x15.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-88-350x23.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-88-480x32.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-88.png 1334w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>\u8fd9\u53e5\u8bdd\u8bf4\u660e\u5f53\u6211\u4eec\u5728\u8ba1\u7b97body\u7684\u540c\u65f6\u6539\u53d8\u4e86\u72b6\u6001\uff0c\u56e0\u4e3a\u72b6\u6001\u7684\u6539\u53d8\uff0c\u53c8\u5bfc\u81f4View\u7684\u91cd\u65b0\u6e32\u67d3\uff0c\u8fd9\u6837\u6b7b\u5faa\u73af\u4e0b\u53bb\uff0c\u6709\u53ef\u80fd\u4f1a\u4ea7\u751f\u672a\u77e5\u7684\u540e\u679c\u3002\u6309\u7167\u6211\u4eec\u7684\u7ecf\u9a8c\uff0c\u6211\u4eec\u53ea\u9700\u8981\u628a<code>self.count += 1<\/code>\u653e\u5230DispatchQueue\u95ed\u5305\u4e2d\u5c31\u53ef\u4ee5\u4e86\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=\"\">DispatchQueue.main.async {\n        self.count += 1\n}\n<\/pre>\n\n\n\n<p>\u8fd9\u4e48\u505a\uff0c\u5c31\u4e0d\u4f1a\u4ea7\u751f\u8fd0\u884c\u65f6\u7684\u63d0\u9192\u4fe1\u606f\uff0c\u4f46\u4ecd\u7136\u6709\u5f88\u5927\u7684\u95ee\u9898\uff0c\u4e3a\u4e86\u8ba9\u5927\u5bb6\u770b\u5230OutOfControlView\u5237\u65b0view\u5bf9CPU\u7684\u4e25\u91cd\u6d88\u8017\uff0c\u6211\u4eec\u5199\u4e00\u4e2a\u80fd\u591f\u663e\u793aCPU\u4f7f\u7528\u767e\u5206\u6bd4\u7684View\uff0c\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"326\" height=\"264\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/1914952-d532682dc16a7689.gif\" alt=\"\" class=\"wp-image-788\"\/><\/figure><\/div>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8ba1\u6570\u5668\u4e0d\u65ad\u7684\u589e\u52a0\uff0cCPU\u4f7f\u7528\u7387\u5f88\u9ad8\uff0c\u8bf4\u660eOutOfControlView\u4e00\u76f4\u4e0d\u65ad\u7684\u5237\u65b0\uff0c\u4e0a\u8fb9\u6548\u679c\u7684\u5b9e\u73b0\u4ee3\u7801\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 Example1: View {\n    @State private var show = false\n\n    var body: some View {\n        VStack {\n            CPUWheel()\n                .frame(height: 150)\n\n            if show {\n                OutOfControlView()\n            }\n\n            Button(self.show ? \"\u9690\u85cf\" : \"\u663e\u793a\") {\n                self.show.toggle()\n            }\n        }\n    }\n}\n\nstruct OutOfControlView: View {\n    @State private var count: Int = 0\n\n    var body: some View {\n        DispatchQueue.main.async {\n            self.count += 1\n        }\n\n        return Text(\"\u8ba1\u7b97\u6b21\u6570\uff1a\\(self.count)\")\n            .multilineTextAlignment(.center)\n    }\n}<\/pre>\n\n\n\n<p>\u90a3\u4e48\u4e3a\u4ec0\u4e48\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u4e86<code>DispatchQueue.main.async{}<\/code>, \u8fd8\u6709\u95ee\u9898\u5462\uff1f\u539f\u56e0\u5728\u4e8e\uff1a<\/p>\n\n\n\n<ul><li><code>DispatchQueue.main.async<\/code>\u662f\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\uff0c\u5c31\u8ddf\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u4e00\u6837\uff0c\u5728\u8ba1\u7b97body\u7684\u65f6\u5019\uff0c\u5e76\u4e0d\u4f1a\u76f4\u63a5\u6267\u884c\u3002<\/li><li>\u5f53body\u8ba1\u7b97\u5b8c\u6210\u540e\u624d\u4f1a\u6267\u884c<code>DispatchQueue.main.async<\/code>\u4e2d\u7684\u4ee3\u7801\uff0c\u8fd9\u65f6\u5019\u72b6\u6001\u4fee\u6539\u4e86\uff0c\u53c8\u89e6\u53d1\u4e86View\u7684\u5237\u65b0\u3002<\/li><li>\u4e00\u76f4\u91cd\u590d\u5faa\u73af\u4e0a\u8fb9\u4e24\u4e2a\u8fc7\u7a0b\u3002<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">3. <strong>\u5982\u4f55\u6253\u7834\u4e0a\u8fb9\u7684\u6b7b\u5faa\u73af\u5462\uff1f<\/strong><\/h4>\n\n\n\n<p>\u6211\u4eec\u4e0d\u518d\u7528\u4e0a\u8fb9\u7684\u8fd9\u4e2a\u4f8b\u5b50\u6f14\u793a\uff0c\u5927\u5bb6\u5148\u770b\u4e0b\u8fb9\u8fd9\u4e2a\u6548\u679c\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\/2020\/08\/1914952-7264836180599066-1.gif\" alt=\"\" class=\"wp-image-791\" width=\"286\" height=\"445\"\/><\/figure><\/div>\n\n\n\n<ul><li>\u968f\u7740\u7bad\u5934\u7684\u65cb\u8f6c\uff0c\u7bad\u5934\u4e0a\u65b9\u7684\u65b9\u5411\u6587\u5b57\u4e5f\u968f\u7740\u66f4\u65b0<\/li><li>CPU\u7684\u4f7f\u7528\u7387\u5e76\u4e0d\u9ad8<\/li><li>\u5728body\u7684\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u5b9e\u65f6\u4fee\u6539\u72b6\u6001<\/li><\/ul>\n\n\n\n<p>\u6e90\u7801\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 Example2: View {\n    @State private var show = false\n    @State private var direction = \"\"\n\n    var body: some View {\n        print(\"\u66f4\u65b0body direction = \\(self.direction) \")\n        return VStack {\n            CPUWheel()\n                .frame(height: 150)\n\n            Text(\"\\(self.direction)\")\n                .font(.largeTitle)\n\n            Image(systemName: \"location.north.fill\")\n                .resizable()\n                .frame(width: 100, height: 100)\n                .foregroundColor(.green)\n                .modifier(RotateEffect(direction: self.$direction, angle: self.show ? 360 : 0))\n\n            Button(\"\u5f00\u59cb\") {\n                withAnimation(.easeInOut(duration: 3.0)) {\n                    self.show.toggle()\n                }\n            }\n            .padding(.top, 50)\n        }\n    }\n}\n\nstruct RotateEffect: GeometryEffect {\n    @Binding var direction: String\n    var angle: Double\n\n    var animatableData: Double {\n        get {\n            angle\n        }\n        set {\n            angle = newValue\n        }\n    }\n\n    func effectValue(size: CGSize) -> ProjectionTransform {\n        DispatchQueue.main.async {\n            self.direction = self.getDirection(self.angle)\n            print(\"\u66f4\u65b0effectValue direction = \\(self.direction) \")\n        }\n\n        let rotation = CGAffineTransform(rotationAngle: CGFloat(angle * (Double.pi \/ 180.0)))\n        let offset1 = CGAffineTransform(translationX: size.width \/ 2.0, y: size.height \/ 2.0)\n        let offset2 = CGAffineTransform(translationX: -size.width \/ 2.0, y: -size.height \/ 2.0)\n        return ProjectionTransform(offset2.concatenating(rotation).concatenating(offset1))\n    }\n\n    func getDirection(_ angle: Double) -> String {\n        switch angle {\n        case 0..&lt;45:\n            return \"\u5317\"\n        case 45..&lt;135:\n            return \"\u4e1c\"\n        case 135..&lt;225:\n            return \"\u5357\"\n        case 225..&lt;315:\n            return \"\u897f\"\n        default:\n            return \"\u5317\"\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u5173\u4e8e\u4e0a\u8fb9\u7684\u4ee3\u7801\uff0c\u5927\u5bb6\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n\n\n\n<ul><li><code>@Binding var direction: String<\/code>: \u5728RotateEffect\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7Binding\u7684\u65b9\u5f0f\u76f4\u63a5\u4fee\u6539\u72b6\u6001<\/li><li>\u901a\u8fc7getDirection\u6765\u8ba1\u7b97\u67d0\u4e2a\u89d2\u5ea6\u4e0b\u7684\u65b9\u5411<\/li><\/ul>\n\n\n\n<p>\u5f53\u8fdb\u884c\u65cb\u8f6c\u7684\u65f6\u5019\uff0c<code>self.direction<\/code>\u4e00\u76f4\u90fd\u5728\u6539\u53d8\uff0c\u4f46\u4e3a\u4ec0\u4e48\u6ca1\u6709\u9020\u6210CPU\u7684\u8fc7\u5ea6\u6d88\u8017\u5462\uff1f\u6211\u4eec\u5728\u4e0a\u8fb9\u4ee3\u7801\u4e2d\u7684\u4e24\u4e2a\u5730\u65b9\u52a0\u4e86\u6253\u5370\u51fd\u6570\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=\"\">print(\"\u66f4\u65b0body direction = \\(self.direction) \")\nprint(\"\u66f4\u65b0effectValue direction = \\(self.direction) \")\n\n<\/pre>\n\n\n\n<p>\u6253\u5370\u7ed3\u679c\u5982\u4e0b\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\/2020\/08\/image-89-1024x728.png\" alt=\"\" class=\"wp-image-792\" width=\"559\" height=\"397\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-89-1024x728.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-89-300x213.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-89-768x546.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-89-830x590.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-89-230x163.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-89-350x249.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-89-480x341.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-89.png 1306w\" sizes=\"(max-width: 559px) 100vw, 559px\" \/><\/figure><\/div>\n\n\n\n<p>\u901a\u8fc7\u4ed4\u7ec6\u5206\u6790\u4e0a\u8fb9\u7684\u6253\u5370\u7ed3\u679c\uff0c\u6211\u4eec\u5f97\u5230\u5982\u4e0b\u7ed3\u8bba\uff1a<\/p>\n\n\n\n<ul><li><code>\u66f4\u65b0body direction = X<\/code>: \u7cfb\u7edf\u5e76\u4e0d\u662f\u6bcf\u6b21direction\u6539\u53d8\u5c31\u66f4\u65b0body\uff0c\u800c\u662f\u975e\u5e38\u806a\u660e\u7684\u77e5\u9053\u4ec0\u4e48\u65f6\u5019\u9700\u8981\u66f4\u65b0body\u3002<\/li><li>\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u7cfb\u7edf\u5df2\u7ecf\u5e2e\u6211\u4eec\u89c4\u907f\u4e86\u5f88\u591a\u91cd\u590d\u5237\u65b0\u7684\u98ce\u9669\uff0c\u6211\u4eec\u9700\u8981\u7406\u89e3\u5176\u80cc\u540e\u7684\u5237\u65b0\u539f\u7406\uff0c\u624d\u80fd\u5199\u51fa\u66f4\u597d\u6027\u80fd\u7684view\u3002<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. \u53e6\u4e00\u79cd\u6b7b\u5faa\u73af<\/h4>\n\n\n\n<p>\u5373\u4fbf\u7cfb\u7edf\u5728\u5904\u7406\u66f4\u65b0\u95ee\u9898\u4e0a\u5df2\u7ecf\u8db3\u591f\u806a\u660e\u4e86\uff0c\u4f46\u6211\u4eec\u5728\u7f16\u7801\u7684\u65f6\u5019\uff0c\u8fd8\u662f\u8981\u5341\u5206\u5c0f\u5fc3\u3002\u6bcf\u5f53\u5728body\u4e2d\u66f4\u65b0\u6570\u636e\u7684\u65f6\u5019\uff0c\u90fd\u9700\u8981\u4ed4\u7ec6\u5206\u6790\u6574\u4e2a\u66f4\u65b0\u8fc7\u7a0b\uff0c\u4e0b\u8fb9\u6f14\u793a\u53e6\u4e00\u4e2a\u4f1a\u4ea7\u751f\u6b7b\u5faa\u73af\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"518\" height=\"131\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/1914952-e7cb454f33227be4.gif\" alt=\"\" class=\"wp-image-795\"\/><\/figure><\/div>\n\n\n\n<p>\u4ee3\u7801\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 Example3: View {\n    @State private var width: CGFloat = 0.0\n    \n    var body: some View {\n        Text(\"Width = \\(self.width)\")\n            .font(.largeTitle)\n            .background(WidthGetter(width: self.$width))\n    }\n    \n    struct WidthGetter: View {\n        @Binding var width: CGFloat\n        \n        var body: some View {\n            GeometryReader { proxy -> AnyView in\n                DispatchQueue.main.async {\n                    self.width = proxy.frame(in: .local).width\n                    print(self.width)\n                }\n                return AnyView(Color.clear)\n            }\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u5728WidthGetter\u4e2d\u4fee\u6539\u72b6\u6001width\u7684\u65f6\u5019\uff0cExample3\u90fd\u9700\u8981\u91cd\u65b0\u5237\u65b0body\uff0c\u7531\u4e8e\u6570\u5b57\u7684\u5bbd\u5ea6\u90fd\u4e0d\u4e00\u6837\uff0c\u9020\u6210\u4e86\u6b7b\u5faa\u73af\uff0c\u6211\u4eec\u770b\u4e00\u4e0b\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"486\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-90-1024x486.png\" alt=\"\" class=\"wp-image-796\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-90-1024x486.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-90-300x142.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-90-768x365.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-90-830x394.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-90-230x109.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-90-350x166.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-90-480x228.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-90.png 1272w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0cwidth\u5728\u8fd9\u51e0\u4e2a\u6570\u503c\u4e4b\u95f4\u4e0d\u65ad\u5207\u6362\uff0c\u5982\u679c\u6211\u4eec\u56fa\u5b9a\u6b7b\u6bcf\u4e2a\u6570\u5b57\u7684\u5bbd\u5ea6\uff0c\u5c31\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\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=\"\">    var body: some View {\n        Text(\"Width = \\(self.width)\")\n            .font(.custom(\"Cochin\", size: 30))\n            .background(WidthGetter(width: self.$width))\n    }<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">\u603b\u7ed3<\/h4>\n\n\n\n<ul><li>\u5c3d\u53ef\u80fd\u907f\u514d\u4e00\u8fb9\u66f4\u65b0body\uff0c\u4e00\u8fb9\u4fee\u6539\u72b6\u6001<\/li><li>\u4f7f\u7528<code>DispatchQueue.main.async{}<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u628a\u72b6\u6001\u7684\u4fee\u6539\u65f6\u673a\u653e\u5230body\u8ba1\u7b97\u5b8c\u6210\u4e4b\u540e<\/li><li>\u5373\u4fbf\u4f7f\u7528<code>DispatchQueue.main.async{}<\/code>\uff0c\u4e5f\u6709\u53ef\u80fd\u4f1a\u5b58\u5728\u95ee\u9898<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u89e3\u5728SwiftUI\u4e2d\u5982\u4f55\u5b89\u5168\u7684\u66f4\u65b0View\uff0c\u80fd\u591f\u8ba9\u5927\u5bb6\u660e\u767dSwiftUI\u4e2dView\u7684\u5237\u65b0\u76f8\u5173\u7684\u539f [&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\/786"}],"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=786"}],"version-history":[{"count":7,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/786\/revisions"}],"predecessor-version":[{"id":801,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/786\/revisions\/801"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}