{"id":439,"date":"2020-08-07T14:26:44","date_gmt":"2020-08-07T06:26:44","guid":{"rendered":"http:\/\/123.57.164.21\/?p=439"},"modified":"2020-08-07T15:14:36","modified_gmt":"2020-08-07T07:14:36","slug":"swiftui-list-drag-drop","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=439","title":{"rendered":"SwiftUI List Drag &#038; Drop"},"content":{"rendered":"\n<p>\u6709\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5e0c\u671b\u80fd\u901a\u8fc7\u62d6\u62fdList\u4e0a\u7684\u9879\u76ee\u3002\u8c03\u6574List\u7684\u987a\u5e8f\uff0c\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u7b80\u5355\u7684\u5b9e\u73b0\u3002<\/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-6-1024x748.png\" alt=\"\" class=\"wp-image-445\" width=\"546\" height=\"398\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-6-1024x748.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-6-300x219.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-6-768x561.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-6-1536x1123.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-6-830x607.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-6-550x400.png 550w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-6-230x168.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-6-350x256.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-6-480x351.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-6.png 1880w\" sizes=\"(max-width: 546px) 100vw, 546px\" \/><\/figure><\/div>\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-7-1024x749.png\" alt=\"\" class=\"wp-image-446\" width=\"557\" height=\"407\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-7-1024x749.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-7-300x219.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-7-768x562.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-7-1536x1123.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-7-830x607.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-7-230x168.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-7-350x256.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-7-480x351.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-7.png 1674w\" sizes=\"(max-width: 557px) 100vw, 557px\" \/><\/figure><\/div>\n\n\n\n<p><\/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=\"\">import SwiftUI\n\nstruct ContentView: View {\n    @State private var fruits = [\"Point1\", \"Point2\", \"Point3\"]\n    @State private var isEditable = false\n\n    var body: some View {\n        List {\n            ForEach(fruits, id: \\.self) { user in\n                HStack(spacing: 20) {\n                    Text(user).foregroundColor(.white)\n                        .font(.largeTitle)\n                        .frame(width: 200, height: 200).background(Color.green)\n                    ScrollView(.horizontal) {\n                        HStack(spacing: 20) {\n\n                            ForEach(0..&lt;10) {\n                                Text(\"Item \\($0)\")\n                                    .foregroundColor(.white)\n                                    .font(.largeTitle)\n                                    .frame(width: 200, height: 200)\n                                    .background(Color.red)\n                            }\n                        }\n                    }.allowsHitTesting(false) \/\/ \u975e\u5e38\u91cd\u8981\uff0c\u963b\u6b62ScrollView\u7684\u70b9\u51fb\u4e8b\u4ef6\uff0c\u5426\u5219\u4f1a\u548cList\u7684\u51b2\u7a81\u3002\n\n                }\n            } .onMove(perform: move)\n                \/\/ \u957f\u6309\u65f6\u6539\u53d8List\u7684\u7f16\u8f91\u6a21\u5f0f\u3002\n                .onLongPressGesture {\n                    withAnimation {\n                        self.isEditable = true\n                    }\n            }\n        }\n\/\/ \u901a\u8fc7\u8bbe\u5b9aenvironment\u7684 editMode\u7684\u503c\uff0c\u6765\u6539\u53d8List\u7684\u7f16\u8f91\u6a21\u5f0f\u3002\n.environment(\\.editMode, isEditable ? .constant(.active) : .constant(.inactive))\n\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","protected":false},"excerpt":{"rendered":"<p>\u6709\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5e0c\u671b\u80fd\u901a\u8fc7\u62d6\u62fdList\u4e0a\u7684\u9879\u76ee\u3002\u8c03\u6574List\u7684\u987a\u5e8f\uff0c\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u7b80\u5355\u7684\u5b9e\u73b0\u3002<\/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\/439"}],"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=439"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/439\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/439\/revisions\/447"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}