Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift – Collectionview交互式单元交换大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我已经实现了 here所描述的集合视图.正如您所看到的,它使用了iOS 9中提供的集合视图的单元的交互式重新排序.但问题是,我无法控制单元格的重新排序. 说细胞是这样的 – 1 2 3 4 5 6 7 8 9 10 11 12 我想只交换单元格6和4.所以在重新排序后,单元格将是 1 2 3 6 5 4 7 8 9 10 11 12 在这里,在教程中,在程序的开头,集合
@H_404_4@
我已经实现了 here所描述的集合视图.正如您所看到的,它使用了iOS 9中提供的集合视图的单元的交互式重新排序.但问题是,我无法控制单元格的重新排序.

说细胞是这样的

1  2  3  4
5  6  7  8
9  10 11 12

我想只交换单元格6和4.所以在重新排序后,单元格将是

1  2  3  6
5  4  7  8
9  10 11 12

在这里,在教程中,在程序的开头,集合视图是这样的

swift – Collectionview交互式单元交换

如果我把星巴克放在Rose Tyler之上,就会发生这种情况 –

swift – Collectionview交互式单元交换

请注意,Sarah Connor有星巴克之地.

我想控制细胞的重新排序,这样Rose Tyler和星巴克的位置就会被交换掉.

我怎么做?

@H_404_4@

解决方法

简单的解决方案是通过简单地沿着屏幕中的平移移动单元格来实现您自己的交互式单元格排序行为,并在手势结束于另一个单元格的位置时进行交换(使用您自己的动画和数据源更新).这是一个工作样本:

class ViewController: UIViewController,UICollectionViewDatasource {

    var arr: [String] = (0...100).map { return "\($0)" }

    lazy var collectionView: UICollectionView =  {
        let layout = UICollectionViewFlowLayout()
        let cv: UICollectionView = UICollectionView(frame: self.view.bounds,collectionViewLayout: layout)
        cv.register(Cell.self,forCellWithReusEIDentifier: Cell.id)
        layout.itemSize = CGSize(width: view.bounds.width/3.5,height: 100)
        cv.datasource = self
        cv.addGestureRecognizer(longPressGesturE)
        return cv
    }()

    lazy var longPressGesture: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self,action: #SELEctor(self.handleLongGesture(gesture:)))

    private var movingCell: MovingCell?

    override func viewDidLoad() {
        super.viewDidLoad()
        view = collectionView
    }

    @objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {

        var cell: (UICollectionViewCell?,IndexPath?) {
            guard let indexPath = collectionView.indexPathForItem(at: gesture.LOCATIOn(in: collectionView)),let cell = collectionView.cellForItem(at: indexPath) else { return (nil,nil) }
            return (cell,indexPath)
        }

        switch(gesture.statE) {

        case .began:
            movingCell = MovingCell(cell: cell.0,originalLOCATIOn: cell.0?.center,indexPath: cell.1)
            break
        case .changed:

            /// Make sure moving cell floats above its siblings.
            movingCell?.cell.layer.zPosition = 100
            movingCell?.cell.center = gesture.LOCATIOn(in: gesture.view!)

            break
        case .ended:

            swapMovingCellWith(cell: cell.0,at: cell.1)
            movingCell = nil
        default:
            movingCell?.reset()
            movingCell = nil
        }
    }

    func swapMovingCellWith(cell: UICollectionViewCell?,at indexPath: IndexPath?) {
        guard let cell = cell,let moving = movingCell else {
            movingCell?.reset()
            return
        }

        // update data source
        arr.swapAt(moving.indexPath.row,indexPath!.row)

        // swap cells
        animate(moving: moving.cell,to: cell)
    }

    func animate(moving movingCell: UICollectionViewCell,to cell: UICollectionViewCell) {
        longPressGesture.isEnabled = false

        UIView.animate(withDuration: 0.4,delay: 0,usingSpringWithDamping: 0.1,initialSpringVeLocity: 0.7,options: UIViewAnimationOptions.allowUserInteraction,animations: {
            movingCell.center = cell.center
            cell.center = movingCell.center
        }) { _ in
            self.collectionView.reloadData()
            self.longPressGesture.isEnabled = true
        }
    }

    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: int) -> Int {
        return arr.count
    }

    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: Cell = collectionView.dequeueReusableCell(withReusEIDentifier: Cell.id,for: indexPath) as! Cell
        cell.titleLable.text = arr[indexPath.row]
        return cell
    }

    private struct MovingCell {
        let cell: UICollectionViewCell
        let originalLOCATIOn: CGPoint
        let indexPath: IndexPath

        init?(cell: UICollectionViewCell?,originalLOCATIOn: CGPoint?,indexPath: IndexPath?) {
            guard cell != nil,originalLOCATIOn != nil,indexPath != nil else { return nil }
            self.cell = cell!
            self.originalLOCATIOn = originalLOCATIOn!
            self.indexPath = indexPath!
        }

        func reset() {
            cell.center = originalLOCATIOn
        }
    }

    final class Cell: UICollectionViewCell {
        static let id: String = "CellId"

        lazy var titleLable: UILabel = UILabel(frame: CGRect(x: 0,y: 20,width: self.bounds.width,height: 30))

        override init(frame: CGRect) {
            super.init(frame: framE)
            addSubview(titleLablE)
            titleLable.BACkgroundColor = .green
            BACkgroundColor = .white
        }

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    }
}
@H_404_4@ @H_404_4@
@H_404_4@
@H_404_4@

大佬总结

以上是大佬教程为你收集整理的swift – Collectionview交互式单元交换全部内容,希望文章能够帮你解决swift – Collectionview交互式单元交换所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。