CHTCollectionViewWaterfallLayout.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  2. // UICollectionViewWaterfallLayout.h
  3. //
  4. // Created by Nelson on 12/11/19.
  5. // Copyright (c) 2012 Nelson Tai. All rights reserved.
  6. //
  7. #import <UIKit/UIKit.h>
  8. /**
  9. * Enumerated structure to define direction in which items can be rendered.
  10. */
  11. typedef NS_ENUM (NSUInteger, CHTCollectionViewWaterfallLayoutItemRenderDirection) {
  12. CHTCollectionViewWaterfallLayoutItemRenderDirectionShortestFirst,
  13. CHTCollectionViewWaterfallLayoutItemRenderDirectionLeftToRight,
  14. CHTCollectionViewWaterfallLayoutItemRenderDirectionRightToLeft
  15. };
  16. /**
  17. * Constants that specify the types of supplementary views that can be presented using a waterfall layout.
  18. */
  19. /// A supplementary view that identifies the header for a given section.
  20. extern NSString *const CHTCollectionElementKindSectionHeader;
  21. /// A supplementary view that identifies the footer for a given section.
  22. extern NSString *const CHTCollectionElementKindSectionFooter;
  23. #pragma mark - CHTCollectionViewDelegateWaterfallLayout
  24. @class CHTCollectionViewWaterfallLayout;
  25. /**
  26. * The CHTCollectionViewDelegateWaterfallLayout protocol defines methods that let you coordinate with a
  27. * CHTCollectionViewWaterfallLayout object to implement a waterfall-based layout.
  28. * The methods of this protocol define the size of items.
  29. *
  30. * The waterfall layout object expects the collection view’s delegate object to adopt this protocol.
  31. * Therefore, implement this protocol on object assigned to your collection view’s delegate property.
  32. */
  33. @protocol CHTCollectionViewDelegateWaterfallLayout <UICollectionViewDelegate>
  34. @required
  35. /**
  36. * Asks the delegate for the size of the specified item’s cell.
  37. *
  38. * @param collectionView
  39. * The collection view object displaying the waterfall layout.
  40. * @param collectionViewLayout
  41. * The layout object requesting the information.
  42. * @param indexPath
  43. * The index path of the item.
  44. *
  45. * @return
  46. * The original size of the specified item. Both width and height must be greater than 0.
  47. */
  48. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
  49. @optional
  50. /**
  51. * Asks the delegate for the column count in a section
  52. *
  53. * @param collectionView
  54. * The collection view object displaying the waterfall layout.
  55. * @param collectionViewLayout
  56. * The layout object requesting the information.
  57. * @param section
  58. * The section.
  59. *
  60. * @return
  61. * The original column count for that section. Must be greater than 0.
  62. */
  63. - (NSInteger)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout columnCountForSection:(NSInteger)section;
  64. /**
  65. * Asks the delegate for the height of the header view in the specified section.
  66. *
  67. * @param collectionView
  68. * The collection view object displaying the waterfall layout.
  69. * @param collectionViewLayout
  70. * The layout object requesting the information.
  71. * @param section
  72. * The index of the section whose header size is being requested.
  73. *
  74. * @return
  75. * The height of the header. If you return 0, no header is added.
  76. *
  77. * @discussion
  78. * If you do not implement this method, the waterfall layout uses the value in its headerHeight property to set the size of the header.
  79. *
  80. * @see
  81. * headerHeight
  82. */
  83. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForHeaderInSection:(NSInteger)section;
  84. /**
  85. * Asks the delegate for the height of the footer view in the specified section.
  86. *
  87. * @param collectionView
  88. * The collection view object displaying the waterfall layout.
  89. * @param collectionViewLayout
  90. * The layout object requesting the information.
  91. * @param section
  92. * The index of the section whose header size is being requested.
  93. *
  94. * @return
  95. * The height of the footer. If you return 0, no footer is added.
  96. *
  97. * @discussion
  98. * If you do not implement this method, the waterfall layout uses the value in its footerHeight property to set the size of the footer.
  99. *
  100. * @see
  101. * footerHeight
  102. */
  103. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForFooterInSection:(NSInteger)section;
  104. /**
  105. * Asks the delegate for the insets in the specified section.
  106. *
  107. * @param collectionView
  108. * The collection view object displaying the waterfall layout.
  109. * @param collectionViewLayout
  110. * The layout object requesting the information.
  111. * @param section
  112. * The index of the section whose insets are being requested.
  113. *
  114. * @discussion
  115. * If you do not implement this method, the waterfall layout uses the value in its sectionInset property.
  116. *
  117. * @return
  118. * The insets for the section.
  119. */
  120. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
  121. /**
  122. * Asks the delegate for the header insets in the specified section.
  123. *
  124. * @param collectionView
  125. * The collection view object displaying the waterfall layout.
  126. * @param collectionViewLayout
  127. * The layout object requesting the information.
  128. * @param section
  129. * The index of the section whose header insets are being requested.
  130. *
  131. * @discussion
  132. * If you do not implement this method, the waterfall layout uses the value in its headerInset property.
  133. *
  134. * @return
  135. * The headerInsets for the section.
  136. */
  137. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForHeaderInSection:(NSInteger)section;
  138. /**
  139. * Asks the delegate for the footer insets in the specified section.
  140. *
  141. * @param collectionView
  142. * The collection view object displaying the waterfall layout.
  143. * @param collectionViewLayout
  144. * The layout object requesting the information.
  145. * @param section
  146. * The index of the section whose footer insets are being requested.
  147. *
  148. * @discussion
  149. * If you do not implement this method, the waterfall layout uses the value in its footerInset property.
  150. *
  151. * @return
  152. * The footerInsets for the section.
  153. */
  154. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForFooterInSection:(NSInteger)section;
  155. /**
  156. * Asks the delegate for the minimum spacing between two items in the same column
  157. * in the specified section. If this method is not implemented, the
  158. * minimumInteritemSpacing property is used for all sections.
  159. *
  160. * @param collectionView
  161. * The collection view object displaying the waterfall layout.
  162. * @param collectionViewLayout
  163. * The layout object requesting the information.
  164. * @param section
  165. * The index of the section whose minimum interitem spacing is being requested.
  166. *
  167. * @discussion
  168. * If you do not implement this method, the waterfall layout uses the value in its minimumInteritemSpacing property to determine the amount of space between items in the same column.
  169. *
  170. * @return
  171. * The minimum interitem spacing.
  172. */
  173. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
  174. /**
  175. * Asks the delegate for the minimum spacing between colums in a secified section. If this method is not implemented, the
  176. * minimumColumnSpacing property is used for all sections.
  177. *
  178. * @param collectionView
  179. * The collection view object displaying the waterfall layout.
  180. * @param collectionViewLayout
  181. * The layout object requesting the information.
  182. * @param section
  183. * The index of the section whose minimum interitem spacing is being requested.
  184. *
  185. * @discussion
  186. * If you do not implement this method, the waterfall layout uses the value in its minimumColumnSpacing property to determine the amount of space between columns in each section.
  187. *
  188. * @return
  189. * The minimum spacing between each column.
  190. */
  191. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumColumnSpacingForSectionAtIndex:(NSInteger)section;
  192. @end
  193. #pragma mark - CHTCollectionViewWaterfallLayout
  194. /**
  195. * The CHTCollectionViewWaterfallLayout class is a concrete layout object that organizes items into waterfall-based grids
  196. * with optional header and footer views for each section.
  197. *
  198. * A waterfall layout works with the collection view’s delegate object to determine the size of items, headers, and footers
  199. * in each section. That delegate object must conform to the `CHTCollectionViewDelegateWaterfallLayout` protocol.
  200. *
  201. * Each section in a waterfall layout can have its own custom header and footer. To configure the header or footer for a view,
  202. * you must configure the height of the header or footer to be non zero. You can do this by implementing the appropriate delegate
  203. * methods or by assigning appropriate values to the `headerHeight` and `footerHeight` properties.
  204. * If the header or footer height is 0, the corresponding view is not added to the collection view.
  205. *
  206. * @note CHTCollectionViewWaterfallLayout doesn't support decoration view, and it supports vertical scrolling direction only.
  207. */
  208. @interface CHTCollectionViewWaterfallLayout : UICollectionViewLayout
  209. @property(nonatomic,copy)NSArray*lockingHeadSections;//锁定区头的索引数组NSSting类型
  210. /**
  211. * @brief How many columns for this layout.
  212. * @discussion Default: 2
  213. */
  214. @property (nonatomic, assign) NSInteger columnCount;
  215. /**
  216. * @brief The minimum spacing to use between successive columns.
  217. * @discussion Default: 10.0
  218. */
  219. @property (nonatomic, assign) CGFloat minimumColumnSpacing;
  220. /**
  221. * @brief The minimum spacing to use between items in the same column.
  222. * @discussion Default: 10.0
  223. * @note This spacing is not applied to the space between header and columns or between columns and footer.
  224. */
  225. @property (nonatomic, assign) CGFloat minimumInteritemSpacing;
  226. /**
  227. * @brief Height for section header
  228. * @discussion
  229. * If your collectionView's delegate doesn't implement `collectionView:layout:heightForHeaderInSection:`,
  230. * then this value will be used.
  231. *
  232. * Default: 0
  233. */
  234. @property (nonatomic, assign) CGFloat headerHeight;
  235. /**
  236. * @brief Height for section footer
  237. * @discussion
  238. * If your collectionView's delegate doesn't implement `collectionView:layout:heightForFooterInSection:`,
  239. * then this value will be used.
  240. *
  241. * Default: 0
  242. */
  243. @property (nonatomic, assign) CGFloat footerHeight;
  244. /**
  245. * @brief The margins that are used to lay out the header for each section.
  246. * @discussion
  247. * These insets are applied to the headers in each section.
  248. * They represent the distance between the top of the collection view and the top of the content items
  249. * They also indicate the spacing on either side of the header. They do not affect the size of the headers or footers themselves.
  250. *
  251. * Default: UIEdgeInsetsZero
  252. */
  253. @property (nonatomic, assign) UIEdgeInsets headerInset;
  254. /**
  255. * @brief The margins that are used to lay out the footer for each section.
  256. * @discussion
  257. * These insets are applied to the footers in each section.
  258. * They represent the distance between the top of the collection view and the top of the content items
  259. * They also indicate the spacing on either side of the footer. They do not affect the size of the headers or footers themselves.
  260. *
  261. * Default: UIEdgeInsetsZero
  262. */
  263. @property (nonatomic, assign) UIEdgeInsets footerInset;
  264. /**
  265. * @brief The margins that are used to lay out content in each section.
  266. * @discussion
  267. * Section insets are margins applied only to the items in the section.
  268. * They represent the distance between the header view and the columns and between the columns and the footer view.
  269. * They also indicate the spacing on either side of columns. They do not affect the size of the headers or footers themselves.
  270. *
  271. * Default: UIEdgeInsetsZero
  272. */
  273. @property (nonatomic, assign) UIEdgeInsets sectionInset;
  274. /**
  275. * @brief The direction in which items will be rendered in subsequent rows.
  276. * @discussion
  277. * The direction in which each item is rendered. This could be left to right (CHTCollectionViewWaterfallLayoutItemRenderDirectionLeftToRight), right to left (CHTCollectionViewWaterfallLayoutItemRenderDirectionRightToLeft), or shortest column fills first (CHTCollectionViewWaterfallLayoutItemRenderDirectionShortestFirst).
  278. *
  279. * Default: CHTCollectionViewWaterfallLayoutItemRenderDirectionShortestFirst
  280. */
  281. @property (nonatomic, assign) CHTCollectionViewWaterfallLayoutItemRenderDirection itemRenderDirection;
  282. /**
  283. * @brief The minimum height of the collection view's content.
  284. * @discussion
  285. * The minimum height of the collection view's content. This could be used to allow hidden headers with no content.
  286. *
  287. * Default: 0.f
  288. */
  289. @property (nonatomic, assign) CGFloat minimumContentHeight;
  290. /**
  291. * @brief The calculated width of an item in the specified section.
  292. * @discussion
  293. * The width of an item is calculated based on number of columns, the collection view width, and the horizontal insets for that section.
  294. */
  295. - (CGFloat)itemWidthInSectionAtIndex:(NSInteger)section;
  296. @end