项目作者: Antondomashnev

项目描述 :
ADMozaicCollectionViewLayout is yet another UICollectionViewLayout subclass that implements "brick", "mozaic" or Pinterest style layout.
高级语言: Swift
项目地址: git://github.com/Antondomashnev/ADMozaicCollectionViewLayout.git
创建时间: 2016-06-03T22:24:42Z
项目社区:https://github.com/Antondomashnev/ADMozaicCollectionViewLayout

开源协议:MIT License

下载


ADMozaicCollectionViewLayout


Carthage compatible
CocoaPods Compatible
codebeat badge

What is it

ADMozaicCollectionViewLayout is yet another UICollectionViewLayout subclass that implements “brick” or “mozaic”
layout.

example

Why do anybody need yet another one?

Because there are plenty of kind of the same layouts already:

But this project is pure swift implementation, so if you don’t want to mess up objective-c code and swift you are on the right page. Also, as an advantage compares to another “mozaic” layout - you’re not limited to predefined sizes of cells.

Usage

The idea behind this layout is to split UICollectionView bounds into some kind of “matrix”.
To do this ADMozaikCollectionViewLayout requires height for rows and width for columns in specific section.

  1. /// Designated initializer for `ADMozaikLayout`
  2. ///
  3. /// - Parameter delegate: delegate/datasource for the layout
  4. public init(delegate: ADMozaikLayoutDelegate)

It requires the delegate object conforms to protocol ADMozaikLayoutDelegate.
The first required method is to return the size of each item in layout:

  1. /// Method should return `ADMozaikLayoutSize` for specific indexPath
  2. ///
  3. /// - Parameter collectionView: collection view is using layout
  4. /// - Parameter layout: layout itself
  5. /// - Parameter indexPath: indexPath of item for the size it asks for
  6. ///
  7. /// - Returns: `ADMozaikLayoutSize` struct object describes the size
  8. func collectionView(_ collectionView: UICollectionView, mozaik layout: ADMozaikLayout, mozaikSizeForItemAt indexPath: IndexPath) -> ADMozaikLayoutSize

Where ADMozaikLayoutSize describes the size of each cell in terms of ADMozaikCollectionViewLayout

  1. /**
  2. * Defines the size of the layout item
  3. */
  4. public struct ADMozaikLayoutSize {
  5. /// Columns number that item requires
  6. let columns: Int
  7. /// Rows number that item requires
  8. let rows: Int
  9. }

The second method is to get the geometry information for each specific section of layout:

  1. /// Method should return `ADMozaikLayoutSectionGeometryInfo` to describe specific section's geometry
  2. ///
  3. /// - Parameters:
  4. /// - collectionView: collection view is using layout
  5. /// - layoyt: layout itself
  6. /// - section: section to calculate geometry info for
  7. ///
  8. /// - Returns: `ADMozaikLayoutSectionGeometryInfo` struct object describes the section's geometry
  9. func collectonView(_ collectionView: UICollectionView, mozaik layoyt: ADMozaikLayout, geometryInfoFor section: ADMozaikLayoutSection) -> ADMozaikLayoutSectionGeometryInfo

Where ADMozaikLayoutSectionGeometryInfo describes the all geometry parameters of the section

  1. /**
  2. * Defines the layout's information
  3. */
  4. public struct ADMozaikLayoutSectionGeometryInfo {
  5. /// array of `ADMozaikLayoutColumn` for the layout
  6. let columns: [ADMozaikLayoutColumn]
  7. /// height for each row in points
  8. let rowHeight: CGFloat
  9. /// minimum space between items
  10. let minimumInteritemSpacing: CGFloat
  11. /// minimum space between each row
  12. let minimumLineSpacing: CGFloat
  13. /// Insets for the section from top, left, right, bottom
  14. let sectionInset: UIEdgeInsets
  15. /// Height for header in section
  16. /// Width is currently limited to the collection view width
  17. let headerHeight: CGFloat
  18. /// Height for footer in section
  19. /// Width is currently limited to the collection view width
  20. let footerHeight: CGFloat
  21. }

Content mode options

  1. /// Method should return `ADMozaikLayoutSectionContentMode` to describe specific section's geometry
  2. ///
  3. /// - Parameters:
  4. /// - collectionView: collection view is using layout
  5. /// - layout: layout itself
  6. /// - section: section to return content mode for
  7. ///
  8. /// - Returns: `ADMozaikLayoutSectionContentMode` enum describes the section's content mode and how to position cells
  9. func collectonView(_ collectionView: UICollectionView, mozaik layout: ADMozaikLayout, contentModeFor section: ADMozaikLayoutSection) -> ADMozaikLayoutSectionContentMode

The contentMode option controls the way how the Mozaik layout is filled with items:

  1. fill - tries to fill vacant spaces with item;
  2. ordered - keeps order of the provided items, so empty space can appear in the collection view.

For the complete example please check the example project. Note that current example project is supposed to be run on iPhone 8 screen’s size.

Install

CocoaPods

To integrate ADMozaicCollectionViewLayout into your Xcode project using CocoaPods, specify it in your Podfile:

  1. source 'https://github.com/CocoaPods/Specs.git'
  2. platform :ios, '8.0'
  3. use_frameworks!
  4. pod 'ADMozaicCollectionViewLayout', '~> 4.0'

Carthage

To integrate ADMozaicCollectionViewLayout into your Xcode project using Carthage, specify it in your Cartfile:

  1. github "Antondomashnev/ADMozaicCollectionViewLayout" ~> 4.0

Run carthage update to build the framework and drag the built ADMozaikCollectionViewLayout.framework into your Xcode project.

Migration guide

License

ADMozaicCollectionViewLayout is available under the MIT license. See LICENSE for more information.