{"id":906,"date":"2020-08-19T19:33:42","date_gmt":"2020-08-19T11:33:42","guid":{"rendered":"http:\/\/123.57.164.21\/?p=906"},"modified":"2020-08-19T19:33:42","modified_gmt":"2020-08-19T11:33:42","slug":"swiftui-%e5%88%a9%e7%94%a8uicollectionview-%e5%b9%b6%e4%b8%94%e5%ae%9e%e7%8e%b0%e6%8b%96%e6%8b%bd","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=906","title":{"rendered":"SwiftUI \u5229\u7528UICollectionView \u5e76\u4e14\u5b9e\u73b0\u62d6\u62fd"},"content":{"rendered":"\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 GridView&lt;CellView: View>: UIViewRepresentable {\n\n    let cellView: (Int) -> CellView\n    let proxy: GeometryProxy\n    var numbers: [Int]\n\n    init(_ numbers: [Int], proxy: GeometryProxy, @ViewBuilder cellView: @escaping (Int) -> CellView) {\n        self.proxy = proxy\n        self.cellView = cellView\n        self.numbers = numbers\n    }\n\n    func makeUIView(context: Context) -> UICollectionView {\n\n        let layout = UICollectionViewFlowLayout()\n        layout.scrollDirection = .vertical\n        layout.minimumLineSpacing = 20\n        layout.minimumInteritemSpacing = 10\n        layout.itemSize = CGSize(width: 234, height: 240)\n\n        let collectionView = UICollectionView(frame: proxy.frame(in: .global), collectionViewLayout: layout)\n        collectionView.backgroundColor = bcBodyBackUIColor\n        collectionView.register(GridCellView.self, forCellWithReuseIdentifier: \"CELL\")\n\n        collectionView.dragInteractionEnabled = true\n        collectionView.dataSource = context.coordinator\n        collectionView.delegate = context.coordinator\n        collectionView.contentInset = UIEdgeInsets(top: 28, left: 28, bottom: 28, right: 28)\n        return collectionView\n    }\n\n    func updateUIView(_ uiView: UICollectionView, context: Context) {\n    }\n\n    func makeCoordinator() -> Coordinator {\n        Coordinator(self)\n    }\n\n    class Coordinator: NSObject, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {\n        var parent: GridView\n        var collectionView: UICollectionView?\n\n        init(_ parent: GridView) {\n            self.parent = parent\n        }\n        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {\n            self.collectionView = collectionView\n            return parent.numbers.count\n        }\n\n        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {\n            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: \"CELL\", for: indexPath) as! GridCellView\n            cell.layer.backgroundColor = bcBodyBackUIColor.cgColor\n            cell.backgroundColor = .clear\n            cell.cellView.rootView = AnyView(parent.cellView(parent.numbers[indexPath.row]).fixedSize())\n\n            let tap = UILongPressGestureRecognizer(target: self, action: #selector(delectBtn(_:)))\n            tap.minimumPressDuration = 0.1\n            cell.addGestureRecognizer(tap)\n\n            return cell\n        }\n\n        @objc func delectBtn(_ sender: UILongPressGestureRecognizer) {\n            switch sender.state {\n            case .began:\n                let selectedCellIndex = self.collectionView!.indexPathForItem(at: sender.location(in: collectionView))\n                self.collectionView!.beginInteractiveMovementForItem(at: selectedCellIndex!)\n            case .changed:\n                self.collectionView!.updateInteractiveMovementTargetPosition(sender.location(in: collectionView))\n            case .ended:\n                self.collectionView!.endInteractiveMovement()\n            default:\n                self.collectionView!.cancelInteractiveMovement()\n            }\n        }\n\n        func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {\n            let tempMybook = self.parent.numbers[sourceIndexPath.item]\n            self.parent.numbers.remove(at: sourceIndexPath.item)\n            self.parent.numbers.insert(tempMybook, at: destinationIndexPath.item)\n        }\n\n    }\n}<\/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=\"\">class GridCellView: UICollectionViewCell {\n    public var cellView = UIHostingController(rootView: AnyView(EmptyView()))\n    public override init(frame: CGRect) {\n        super.init(frame: frame)\n        configure()\n    }\n    public required init?(coder aDecoder: NSCoder) {\n        super.init(coder: aDecoder)\n        configure()\n    }\n    private func configure() {\n        contentView.addSubview(cellView.view)\n        cellView.view.translatesAutoresizingMaskIntoConstraints = false\n        NSLayoutConstraint.activate([\n            cellView.view.leftAnchor.constraint(equalTo: contentView.leftAnchor),\n            cellView.view.rightAnchor.constraint(equalTo: contentView.rightAnchor),\n            cellView.view.topAnchor.constraint(equalTo: contentView.topAnchor),\n            cellView.view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)\n            ])\n        cellView.view.layer.masksToBounds = true\n        cellView.view.layer.backgroundColor = bcBodyBackUIColor.cgColor\n    }\n}<\/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 MyView: View {\n\n    var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n\n    var body: some View {\n\n\n        GeometryReader { proxy in\n            GridView(self.numbers, proxy: proxy) { number in\n                Text(String(number))\n            }\n        }.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 150)\n\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","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\/906"}],"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=906"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/906\/revisions"}],"predecessor-version":[{"id":907,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/906\/revisions\/907"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}