说细胞是这样的 –
1 2 3 45 6 7 89 10 11 12
我想只交换单元格6和4.所以在重新排序后,单元格将是
1 2 3 65 4 7 89 10 11 12
在这里,在教程中,在程序的开头,集合视图是这样的 –
如果我把星巴克放在Rose Tyler之上,就会发生这种情况 –
请注意,Sarah Connor有星巴克之地.
我想控制细胞的重新排序,这样Rose Tyler和星巴克的位置就会被交换掉.
我怎么做?
解决方法 简单的解决方案是通过简单地沿着屏幕中的平移移动单元格来实现您自己的交互式单元格排序行为,并在手势结束于另一个单元格的位置时进行交换(使用您自己的动画和数据源更新).这是一个工作样本:class VIEwController: UIVIEwController,UICollectionVIEwDataSource { var arr: [String] = (0...100).map { return "\()" } 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) } }}总结
以上是内存溢出为你收集整理的swift – Collectionview交互式单元交换全部内容,希望文章能够帮你解决swift – Collectionview交互式单元交换所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)