実現したいこと
TwitterやInstagramのように複数枚画像を添付した際に、添付した画像を長押しで並び替えできるようにしたい。
##ソースコード
Swift
1import UIKit 2import DKImagePickerController 3 4class PictureViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIGestureRecognizerDelegate { 5 6 var assets:[DKAsset]? 7 8 9 @IBOutlet weak var collectionView: UICollectionView! 10 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 addLongTapGesture() 15 16 } 17 @IBAction func tapButton(_ sender: Any) { 18 let pickerController = DKImagePickerController() 19 pickerController.maxSelectableCount = 4 - (assets?.count ?? 0) 20 pickerController.UIDelegate = CustumUIDelegate() 21 pickerController.didSelectAssets = { [unowned self] (assets: [DKAsset]) in 22 self.assets = assets 23 self.collectionView.reloadData() 24 25 } 26 present(pickerController, animated: true, completion: nil) 27 } 28 29 func addLongTapGesture() { 30 let longTapGesture = UILongPressGestureRecognizer(target:self,action:#selector(longTap(gesture:))) 31 collectionView.addGestureRecognizer(longTapGesture) 32 } 33 34 @objc func longTap(gesture:UILongPressGestureRecognizer){ 35 switch gesture.state{ 36 // ロングタップの開始時 37 case.began: 38 guard 39 let selectedIndexPath = collectionView.indexPathForItem(at:gesture.location(in:collectionView))else{ 40 break 41 } 42 collectionView.beginInteractiveMovementForItem(at:selectedIndexPath) 43 // セルの移動中 44 case.changed: 45 collectionView.updateInteractiveMovementTargetPosition(gesture.location(in:gesture.view)) 46 // セルの移動完了時 47 case.ended: 48 collectionView.endInteractiveMovement() 49 default: 50 collectionView.cancelInteractiveMovement() 51 } 52 } 53 54 55 56 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 57 return self.assets?.count ?? 0 58 } 59 60 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 61 let asset = self.assets![indexPath.row] 62 var cell: UICollectionViewCell? 63 var imageView: UIImageView? 64 65 if asset.type == .photo { 66 cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellImage", for: indexPath) 67 imageView = cell?.contentView.viewWithTag(1) as? UIImageView 68 } 69 //表示 70 if let imageView = imageView { 71 72 asset.fetchFullScreenImage(completeBlock: { image, info in 73 imageView.image = image 74 75 }) 76 } 77 return cell! 78 } 79 80 // 並び替えを可とする 81 func collectionView(_collectionView:UICollectionView, canMoveItemAt indexPath:IndexPath)-> Bool { 82 return true 83 } 84 85 func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 86 reorderCells(sourceIndex: sourceIndexPath.item, destinationIndex: destinationIndexPath.item) 87 } 88 89 // MARK: - PRIVATE METHODS 90 91 /// セルの移動 92 /// 93 /// - Parameters: 94 /// - sourceIndex: 移動元の位置 95 /// - destinationIndex: 移動先の位置 96 private func reorderCells(sourceIndex: Int, destinationIndex: Int) { 97 var imageView: [UIImageView]? 98 guard let n = imageView?.remove(at: sourceIndex) else { return } 99 imageView?.insert(n, at: destinationIndex) 100 } 101 102 103 104 105} 106
※画像のサイズがバラバラなのは見逃していただけると助かりますmm
並び替え前 | 並び替え後(一番左の画像を左から2番目に移動) |
---|---|
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/03/24 09:36
2021/03/24 10:17