■やりたいこと
collectionViewを使ってドラッグしたら画像が入れ替わる画面を作りたい。
ただし1番目の画像だけ大きくしたい。
■困っていること
ドラッグしたら画像が消えてしまう(消えてしまうように見える)
1番目の画像を大きくするという実装が邪魔をしているように見える。
>//cellの大きさに合わせて画像もリサイズする
>imageView.frame.size = self.testCell.frame.size
この部分をコメントアウトすれば消えることはない。
■質問
1番目の画像を大きくしても、ドラッグが正常にできるようにしたいにしたいのですが
方法がわからず、教えてください。
import UIKit class ViewController: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate { //collectionView @IBOutlet weak var collectionView: UICollectionView! //imageView var imageView: UIImageView! //Cell var testCell: UICollectionViewCell! // サムネイル画像の名前 var photos = ["cloth1", "cloth2","cloth3","cloth4","cloth5", "cloth6"] override func viewDidLoad() { super.viewDidLoad() //セルの長押しのリスナーの登録 addLongPressGestureListner() } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ // "Cell" はストーリーボードで設定したセルのID testCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell",for: indexPath) // Tag番号を使ってImageViewのインスタンス生成 imageView = testCell.contentView.viewWithTag(1) as? UIImageView // 画像配列の番号で指定された要素の名前の画像をUIImageとする let cellImage = UIImage(named: photos[indexPath.row]) // UIImageをUIImageViewのimageとして設定 imageView.image = cellImage //cellの大きさに合わせて画像もリサイズする imageView.frame.size = self.testCell.frame.size return testCell } func numberOfSections(in collectionView: UICollectionView) -> Int { // section数は1つ return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // 要素数を入れる、要素以上の数字を入れると表示でエラーとなる return photos.count; } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //true で移動を可能にする func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool { return true } //移動した時の挙動 func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { self.reorderCells(sourceIndex: sourceIndexPath.item, destinationIndex: destinationIndexPath.item) } // セルの移動 //sourceIndex: 移動元の位置 //destinationIndex: 移動先の位置 private func reorderCells(sourceIndex: Int, destinationIndex: Int) { DispatchQueue.main.async { let n = self.photos.remove(at: sourceIndex) self.photos.insert(n, at: destinationIndex) } } /// セルの長押しのリスナーの登録 func addLongPressGestureListner() { let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongGesture(gesture:))) collectionView.addGestureRecognizer(longPressGesture) } // セルの長押しジェスチャーのアクション @objc private func handleLongGesture(gesture: UILongPressGestureRecognizer) { switch gesture.state { case .began: guard let selectedIndexPath = collectionView.indexPathForItem(at: gesture.location(in: collectionView)) else { break } collectionView.beginInteractiveMovementForItem(at: selectedIndexPath) case .changed: collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view)) case .ended: collectionView.endInteractiveMovement() default: collectionView.cancelInteractiveMovement() } } } //UICollectionViewDelegateFlowLayoutの拡張 extension ViewController: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { var cellSize1:CGFloat var cellSize2:CGFloat if indexPath.row == 0 { cellSize1 = self.view.bounds.width cellSize2 = self.view.bounds.width/2 }else{ let horizontalSpace:CGFloat = 1 cellSize1 = self.view.bounds.width/5 - horizontalSpace cellSize2 = self.view.bounds.width/5 - horizontalSpace } return CGSize(width: cellSize1, height: cellSize2) } }
あなたの回答
tips
プレビュー