私は現在、UICollectionViewを垂直方向にスクロールしています。私はそれがこのように見えるようにしたいと思います。中間の行が他の2つよりも少し高く始まって、クールで面白い効果を作り出すのです。 これを達成するためのアイデアはありますか?UICollectionViewカスタムレイアウトより上に開始する中間列
3
A
答えて
2
コレクションビューレイアウトはcustom layout classを作成することによって達成することができます。
論:
は、基本的には、コレクションビューが必要なレイアウト情報を求めて、全体的なレイアウトプロセスを管理するためのカスタムレイアウトオブジェクトを直接動作します。
レイアウトプロセス中、コレクションビューはレイアウトオブジェクトの特定のメソッドを呼び出します。これらのメソッドは、アイテムの位置を計算し、コレクションビューに必要な主要情報を提供する機会を提供します。
- prepare():
次のメソッドは、常にレイアウトプロセス中の順序で呼び出されたレイアウト情報
- collectionViewContentSizeを提供するために必要なアップフロントの計算を実行します。ベースのコンテンツ領域全体の全体のサイズを返します。
- layoutAttributesForElements(in:):指定された四角形内にあるセルとビューの属性を返します。
実践:
前提条件:さんは
UICollectionViewLayout
サブクラスを作成してみましょう、私たちは場所にCollectionビューを持っていると データソースおよび代表者で構成されたと仮定。私はそれをHiveLayout
と命名した。ストーリーボードのcollectionView
にも割り当てました。また、// Properties for configuring the layout: the number of columns and the cell padding. fileprivate var numberOfColumns = 3 fileprivate var cellPadding: CGFloat = 10 // Cache the calculated attributes. When you call prepare(), you’ll calculate the attributes for all items and add them to the cache. You can be efficient and query the cache instead of recalculating them every time. fileprivate var cache = [UICollectionViewLayoutAttributes]() // Properties to store the content size. fileprivate var contentHeight: CGFloat = 0 fileprivate var contentWidth: CGFloat { guard let collectionView = collectionView else { return 0 } let insets = collectionView.contentInset return collectionView.bounds.width - (insets.left + insets.right) }
prepare()プロセスにおいて有用であろういくつかの変数が必要になります。私たちは、実際に細胞
override func prepare() { // If cache is empty and the collection view exists – calculate the layout attributes guard cache.isEmpty == true, let collectionView = collectionView else { return } // xOffset: array with the x-coordinate for every column based on the column widths // yOffset: array with the y-position for every column, Using odd-even logic to push the even cell upwards and odd cells down. let columnWidth = contentWidth/CGFloat(numberOfColumns) var xOffset = [CGFloat]() for column in 0 ..< numberOfColumns { xOffset.append(CGFloat(column) * columnWidth) } var column = 0 var yOffset = [CGFloat]() for i in 0..<numberOfColumns { yOffset.append((i % 2 == 0) ? (columnWidth/2) : 0) } for item in 0 ..< collectionView.numberOfItems(inSection: 0) { let indexPath = IndexPath(item: item, section: 0) // Calculate insetFrame that can be set to the attribute let cellHeight = columnWidth - (cellPadding * 2) let height = cellPadding * 2 + cellHeight let frame = CGRect(x: xOffset[column], y: yOffset[column], width: columnWidth, height: height) let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding) // Create an instance of UICollectionViewLayoutAttribute, sets its frame using insetFrame and appends the attributes to cache. let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath) attributes.frame = insetFrame cache.append(attributes) // Update the contentHeight to account for the frame of the newly calculated item. It then advances the yOffset for the current column based on the frame contentHeight = max(contentHeight, frame.maxY) yOffset[column] = yOffset[column] + height column = column < (numberOfColumns - 1) ? (column + 1) : 0 } }
collectionViewContentSizeの属性を計算する場所です:
// Using contentWidth and contentHeight, calculate collectionViewContentSize. override var collectionViewContentSize: CGSize { return CGSize(width: contentWidth, height: contentHeight) }
layoutAttributesForElements(in:):
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { var visibleLayoutAttributes = [UICollectionViewLayoutAttributes]() for attributes in cache { if attributes.frame.intersects(rect) { visibleLayoutAttributes.append(attributes) } } return visibleLayoutAttributes }
layoutAttributesForItem(at:):特定のセル
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { return cache[indexPath.item] }
のための属性を返すために、レイアウトクラスのgistをチェックしてみて下さい。
ここでは動作しています!私はそれを作るだろうか
関連する問題
- 1. カスタムレイアウトのUICollectionViewインターフェイスローテーション
- 2. カスタムレイアウトに含まれるアニメーションの開始方法
- 3. UICollectionView - 垂直スクロール、カスタムレイアウトの水平ページング
- 4. UIScrollViewが開始位置よりも上にならないようにする
- 5. IosでUICollectionViewのカスタムレイアウトを設定する方法
- 6. 正確に時間の開始時にサービスを開始し、その後1時間ごとに繰り返す
- 7. Googleマップ上で、ソース、目的地、開始時間、および(開始時間+時間)を入力としてGoogleマップのユーザー位置を取得する方法
- 8. 開始点、中間点、終了点の上にテキストを含む段階的なSeekbarを作成する方法
- 9. 開始時間と終了時間を1つの開始時間と終了時間に分割する
- 10. CollapsingToolbarLayout:上にカスタムレイアウトを修正します
- 11. サービスの開始を中止する
- 12. スレーブ上で実行中のスレーブXでJenkinsジョブを開始
- 13. フローティングアクションボタンによるアクティビティ開始
- 14. ソート/ソートの開始値を中から指定する - 進行中に開く
- 15. 週別Netezzaグループ化開始日(日曜日)および月間開始
- 16. UICollectionView:中アサーションの失敗 - [UICollectionView _endItemAnimations]
- 17. npm開始ファイル中にエラーが見つかりません
- 18. 取得中にエラーがNPM開始
- 19. 左上の代わりに中間から移動する
- 20. はdivの開始を中間ページにしますが、ユーザーがページを下にスクロールすると、上部に残りますか?
- 21. ページ開始時にタイマーを開始し、DBに時間を記録します
- 22. DownloadManagerキュー - 開始時間
- 23. 広告開始時間
- 24. は、ビューの上部に開始CodeIgniterの
- 25. ポップアップの上にイオン開始モーダル
- 26. SmoothDivScrollをスクロールエリアの中央から開始するようにする
- 27. 時間の途中から開始し、次の数時間に5分ごとに実行するHangfireジョブ
- 28. SQL開始時間で注文する時間時間
- 29. 開始の中括弧{とコロンの間を置換する正規表現
- 30. アンドロイド:どのようにカスタムレイアウト
最初のセルには、助けて幸せになりますあなたは手の込んだことができますか?一番上ではなく、右に1 – user6520705
であること –
最初のセルが今すぐ右側の列に表示されますが、可能であれば中央の列が最初に満たされるようにしたいと思いますか? – user6520705