お世話になります。
現在、UICollectionViewのスクロール終了時に「完全に表示されているCell」のみを取得する処理を考えています。
現在実装している処理
"scrollViewDidEndDecelerating"で"collectionView.visibleCells"から表示されているcellを取得。
問題
スクロールが早い場合、Cellの描画、スクロールが完全に終了する前に"scrollViewDidEndDecelerating"が通ってしまうようで、"collectionView.visibleCells"の中身が0個となってしまう。
したいこと
「完全にUICollecitonViewのスクロールが終了した時」に「collwctionView.visibleCell」からCellを取得。
個人的に問題だと思うところ
画面描写に時間がかかる(特定のCellが表示された場合、Modalで地図を表示)部分があるので、そこで引っかかっている?
ただ、スクロールとは関係ないんじゃ。。。といったところです。
特別な事はしていないのですがソースを記載致します。
swift
1 2 // ============================================== 3 // 4 // 5 // MARK:- CollectionView 6 // 7 // 8 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 9 return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height - 20.0) 10 } 11 12 13 //例: ここでindexPath.row == 0 から indexPaht.row == 15へにした場合。 14 private func collectionViewScrollTo(atIndexPath: IndexPath) { 15 _collectionView.scrollToItem(at: atIndexPath, 16 at: .left, 17 animated: true) 18 _varticalPageControl.setCurrentPage(page:atIndexPath.item) 19 _textSpeaker.stopSpeak() 20 } 21 22 23 private func getJ_BaseCell(for item: Int) -> J_BaseCell { 24 return _collectionView.cellForItem(at: IndexPath(item: item, section:0)) as! J_BaseCell 25 } 26 27 28 // ============================================== 29 // 30 // 31 // MARK:- スクロール終了を検知 32 // 33 // 34 func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { 35 let cell = self.getPerfectVisibleCell() 36 showModalView(cell: cell) 37 38 } 39 func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 40 let cell = self.getPerfectVisibleCell() 41 showModalView(cell: cell) 42 } 43 44 // ============================================== 45 // 46 // 47 // MARK:- 表示されているCellから完全に表示されているCellを取得 48 // 49 // 50 private func getPerfectVisibleCell() ->J_BaseCell { 51 52 53 let perfectVisibleCells = _collectionView.visibleCells.filter { 54 _collectionView.bounds.contains($0.frame) 55 } 56 57 return perfectVisibleCells[0] as! J_BaseCell // スクロールが早い場合。ここでエラーが発生。 58 } 59 60 61
該当箇所で、"perfectVisibleCells.countが0"の時は、
let perfectVisibleCells = _collectionView.visibleCells.filter { _collectionView.bounds.contains($0.frame) }
この処理をし続ける。。。なんて事が出来れば良いのかな...?など勝手に思っております。
どうぞ、よろしくお願い致します。
== 追記 ==
UICollectionViewと同じサイズのCellを使用しています。
(今朝、考えてましたらvisibleCellsが0の理由が追記前の質問だとわかりにくくなってました)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height) }
また、機能としましては「完全に表示されているCell」の"J_BaseCell"のプロパティ"type"を取得して処理を切り分ける。というような物になっております。
getPerfectVisibleCell()で取得出来たCellに対して let type = cell.getType() 以下、typeごとの処理
※UIPageViewControllerのような物をUICollectionViewで実装したい。
表示するViewの順番が数パターンあるので、CollectionViewの方が切り分けが楽かな?と思いこちらを採用しました。
(あまりUIPageViewControllerが得意ではないので...)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/06 00:13