{"id":420,"date":"2020-08-07T14:06:03","date_gmt":"2020-08-07T06:06:03","guid":{"rendered":"http:\/\/123.57.164.21\/?p=420"},"modified":"2020-08-07T14:06:17","modified_gmt":"2020-08-07T06:06:17","slug":"swiftui-environment","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=420","title":{"rendered":"SwiftUI Environment"},"content":{"rendered":"\n<p>Environment \u662f SwiftUI \u4e2d\u4e00\u4e2a\u72ec\u7279\u7684\u529f\u80fd\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u63d0\u4f9b\u7cfb\u7edf\u8303\u56f4\u5185\u7684\u8bbe\u7f6e\u5982 LayoutDirection\uff0cColorScheme\uff0c\u4e5f\u53ef\u4ee5\u63d0\u4f9b\u7279\u5b9a\u4e8e\u67d0\u4e9b View \u7684\u8bbe\u7f6e\u5982 isEnabled\uff0c editMode\uff0cpresentationMode \u7b49\u3002<\/p>\n\n\n\n<p>\u5728SwiftUI\u4e2d\u521b\u5efa\u5e76\u542f\u52a8\u60a8\u7684\u7b2c\u4e00\u4e2aView\u65f6\uff0c\u6846\u67b6\u4f1a\u4e3a\u5176\u751f\u6210Environment\u3002 Environment\u662fSwiftUI\u81ea\u52a8\u521b\u5efa\u7684\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u505a\u4efb\u4f55\u4e8b\u60c5\u3002 \u901a\u8fc7Environment\u53ef\u4ee5\u53d6\u5f97\u548c\u6539\u53d8\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u7684\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">\u901a\u8fc7 @Environment \u5c5e\u6027\u5305\u88c5\u53ef\u4ee5\u83b7\u53d6 Environment \u7684\u503c\u3002<\/pre>\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\n    \/\/ \u901a\u8fc7Environment\u83b7\u53d6\u7cfb\u7edf\u8bbe\u5b9acolorScheme\u7684\u503c\n    @Environment(\\.colorScheme) var colorScheme\n    \n    var body: some View {\n        Group {\n            \/\/ \u6839\u636e\u7cfb\u7edfcolorScheme\u7684\u503c\u6539\u53d8SwiftUI\u7684\u5e03\u5c40\n            if colorScheme == .dark {\n                VStack {\n                    Text(\"Hello\")\n                    Text(\"SwiftUI\")\n\n                } }\n            else {\n                HStack {\n                    Text(\"Hello\")\n                    Text(\"SwiftUI\")\n                }\n            } }\n    }\n\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">\u53ef\u4ee5\u8bbe\u5b9a Environment \u7684\u503c\u3002<\/pre>\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 ContentView: View {\n    @State private var fruits = [\"Apple\", \"Banana\", \"Mango\"]\n    @State private var isEditable = false\n\n    var body: some View {\n        List {\n            ForEach(fruits, id: \\.self) { user in\n                Text(user)\n            }\n                .onMove(perform: move)\n                .onLongPressGesture {\n                    withAnimation {\n                        self.isEditable = true\n                    }\n            }\n        }\n       \/\/ \u8fd9\u91cc\u6539\u53d8\u4e86 environment editMode\u7684\u503c\uff0c\u4ece\u800c\u5207\u6362List\u7684\u7f16\u8f91\u6a21\u5f0f\u548c\u666e\u901a\u6a21\u5f0f\u3002\n        .environment(\\.editMode, isEditable ? .constant(.active) : .constant(.inactive))\n    }\n\n    func move(from source: IndexSet, to destination: Int) {\n        fruits.move(fromOffsets: source, toOffset: destination)\n        withAnimation {\n            isEditable = false\n        }\n    }\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">\u9664\u4e86\u7528\u7cfb\u7edf\u7684Key\uff0cenvironment\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49EnvironmentKey\uff0c \n\u7136\u540e\u5c06\u6240\u9700\u8981\u7684\u8bbe\u7f6e\u653e\u5230 EnvironmentValues \u4e2d\u63d0\u4f9b\u7ed9\u5916\u754c\u4f7f\u7528\u3002<\/pre>\n\n\n\n<ul><li>\u9075\u5b88 EnvironmentKey \u534f\u8bae\uff0c\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002<\/li><\/ul>\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=\"\">\nStruct ColorKey: EnvironmentKey {\n    static var defaultValue: Color = .red\n}\n<\/pre>\n\n\n\n<ul><li>\u6269\u5c55 EnvironmentValues \uff0c\u63d0\u4f9b\u8ba1\u7b97\u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u5c31\u662f\u4f7f\u7528@Environment \u65f6\u7684\u53c2\u6570Key Path\u3002<\/li><\/ul>\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 EnvironmentValues {\n\/\/ \u8ba1\u7b97\u5c5e\u6027\u7c7b\u578b\uff0c\u548cColorKey\u4e2ddefaultValue\u7c7b\u578b\u4e00\u81f4\u3002\n    var customColor: Color { \n        get { self[ColorKey.self] }\n        set { self[ColorKey.self] = newValue }\n    }\n}<\/pre>\n\n\n\n<ul><li>\u8bbe\u7f6ecustomColor\u7684\u503c\u3002<\/li><\/ul>\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=\"\">\n ContentView().environment(\\.customColor, .blue)\n<\/pre>\n\n\n\n<ul><li>\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684customColor<\/li><\/ul>\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=\"\">\nstruct ContentView: View {\n    \/\/ \\.customColor \u662f\u524d\u9762\u8bbe\u7f6e\u7684\u8ba1\u7b97\u5c5e\u6027\n    @Environment(\\.customColor) var customColor\n    var body: some View {\n        HStack {\n            Text(\"Hello\")\n            Text(\"SwiftUI\")\n        }\n            .foregroundColor(customColor) \/\/ \u4f7f\u7528 customColor }\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Environment \u662f SwiftUI \u4e2d\u4e00\u4e2a\u72ec\u7279\u7684\u529f\u80fd\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u63d0\u4f9b\u7cfb\u7edf\u8303\u56f4\u5185\u7684\u8bbe\u7f6e\u5982 LayoutD [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/420"}],"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=420"}],"version-history":[{"count":18,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/420\/revisions"}],"predecessor-version":[{"id":438,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/420\/revisions\/438"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}