質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

0回答

595閲覧

collectionViewでドラッグしたら移動させたいが、移動させると画像が見えなくなることがある

syosinsya_swift

総合スコア62

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/05/27 09:24

■やりたいこと
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) } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問