最近產(chǎn)品出了一個(gè)需要移動(dòng)UICollectionViewCell需求刮萌,其中有點(diǎn)小挫折,與大家分享一下卫袒。
UICollectionView移動(dòng)前提條件:
iOS版本必須大于iOS 9.0
Apple官方文檔叁执,關(guān)于UICollectionView移動(dòng)方法如下:
// Support for reordering
@available(iOS 9.0, *)
open func beginInteractiveMovementForItem(at indexPath: IndexPath) -> Bool // returns NO if reordering was prevented from beginning - otherwise YES
@available(iOS 9.0, *)
open func updateInteractiveMovementTargetPosition(_ targetPosition: CGPoint)
@available(iOS 9.0, *)
open func endInteractiveMovement()
@available(iOS 9.0, *)
open func cancelInteractiveMovement()
進(jìn)入正文,給UICollectionView增加移動(dòng)步驟如下
1. 給collectionView增加長(zhǎng)按手勢(shì)
2. 在手勢(shì)識(shí)別中油猫,設(shè)置各種移動(dòng)事件
3. 在delegate設(shè)置對(duì)應(yīng)cell是否可以移動(dòng)
4. 在delegate中交換數(shù)據(jù)
5. 設(shè)置不可參與移動(dòng)的IndexPath
具體代碼如下:
import DKImagePickerController
class CommodityImageEditVC: UIViewController {
// 選取圖片框架
var assets = [DKAsset]()
let commodityImageEditCellID = "CommodityImageEditCellID"
// 欄加載
lazy var gridView: UICollectionView = {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSize(width: 162.0, height: 162.0)
flowLayout.sectionInset = UIEdgeInsets(top: 20, left: 50, bottom: 20, right: 50)
flowLayout.minimumLineSpacing = 32.0
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
collectionView.backgroundColor = UIColor.white
collectionView.register(CommodityImageEditCell.self, forCellWithReuseIdentifier: commodityImageEditCellID)
collectionView.dataSource = self
collectionView.delegate = self
return collectionView;
}()
// MARK: - life cycle
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
// 設(shè)置表格視圖大小
gridView.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - setup view
func setupView() {
view.addSubview(gridView)
setUpLongPressGesture()
}
/**
給gridView增加長(zhǎng)按事件
*/
func setUpLongPressGesture() {
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressMoving(longPress:)))
gridView.addGestureRecognizer(longPress)
}
}
// MARK: - 長(zhǎng)按手勢(shì)識(shí)別
extension CommodityImageEditVC {
@objc func longPressMoving(longPress: UILongPressGestureRecognizer) {
// 獲取長(zhǎng)按點(diǎn)
let point = longPress.location(in: longPress.view)
if #available(iOS 9, *) {
switch longPress.state {
case .began:
// 根據(jù)長(zhǎng)按點(diǎn)稠茂,獲取對(duì)應(yīng)cell的IndexPath
guard let indexPath = gridView.indexPathForItem(at: point) else {
return
}
gridView.beginInteractiveMovementForItem(at: indexPath)
case .changed:
gridView.updateInteractiveMovementTargetPosition(point)
case .ended:
gridView.endInteractiveMovement()
default:
gridView.cancelInteractiveMovement()
}
}
}
}
// MARK: - dataSource delegate
extension CommodityImageEditVC: UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return assets.count + 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: commodityImageEditCellID, for: indexPath) as? CommodityImageEditCell
if indexPath.row == assets.count {
cell?.cellType = .add
} else {
cell?.cellType = .image
cell?.updateCell(asset: assets[indexPath.row])
}
return cell!
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// 判斷選中的是否為增加圖片選項(xiàng)
guard let cell = collectionView.cellForItem(at: indexPath) as? CommodityImageEditCell else {
return
}
guard cell.cellType == .add else {
return
}
let pickerController = DKImagePickerController()
pickerController.didSelectAssets = {(assets: [DKAsset]) in
self.assets.append(contentsOf: assets)
self.gridView.reloadData()
}
present(pickerController, animated: true, completion: nil)
}
// 設(shè)置最后一個(gè)不可參與移動(dòng)
func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {
if proposedIndexPath.item == assets.count {
return originalIndexPath
}
if originalIndexPath.item == assets.count {
return originalIndexPath
}
return proposedIndexPath
}
// 設(shè)置是否可以移動(dòng)
func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
if indexPath.row == assets.count {
return false
} else {
return true
}
}
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// 移動(dòng)后交換數(shù)據(jù)
let moveData = assets.remove(at: sourceIndexPath.row)
assets.insert(moveData, at: destinationIndexPath.row)
}
}