HorizontalLayout.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // HorizontalLayout.m
  3. // ZYXHorizontalFlowLayout
  4. //
  5. // Created by 张哈哈 on 2018/6/23.
  6. // Copyright © 2018年 zyx. All rights reserved.
  7. //
  8. #import "HorizontalLayout.h"
  9. #define edgeDis 0
  10. @interface HorizontalLayout()
  11. @property (nonatomic,strong) NSMutableArray *attrs;
  12. @property (nonatomic,strong) NSMutableDictionary *pageDict;
  13. @end
  14. @implementation HorizontalLayout
  15. #pragma mark - life cycle
  16. - (instancetype)init {
  17. if (self = [super init]) {
  18. self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  19. }
  20. return self;
  21. }
  22. - (void)prepareLayout {
  23. [super prepareLayout];
  24. [self.attrs removeAllObjects];
  25. // 获取section数量
  26. NSInteger section = [self.collectionView numberOfSections];
  27. for (int i = 0; i < section; i++) {
  28. // 获取当前分区的item数量
  29. NSInteger items = [self.collectionView numberOfItemsInSection:i];
  30. for (int j = 0; j < items; j++) {
  31. // 设置item位置
  32. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:j inSection:i];
  33. UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:indexPath];
  34. [self.attrs addObject:attr];
  35. }
  36. }
  37. }
  38. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
  39. UICollectionViewLayoutAttributes *attr = [super layoutAttributesForItemAtIndexPath:indexPath].copy;
  40. [self resetItemLocation:attr];
  41. return attr;
  42. }
  43. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
  44. return self.attrs;
  45. }
  46. - (CGSize)collectionViewContentSize {
  47. // 将所有section页面数量相加
  48. NSInteger allPagesCount = 0;
  49. for (NSString *page in [self.pageDict allKeys]) {
  50. allPagesCount += allPagesCount + [self.pageDict[page] integerValue];
  51. }
  52. CGFloat width = allPagesCount * self.collectionView.bounds.size.width;
  53. CGFloat hegith = self.collectionView.bounds.size.height;
  54. return CGSizeMake(width, hegith);
  55. }
  56. #pragma mark - private method
  57. // 设置item布局属性
  58. - (void)resetItemLocation:(UICollectionViewLayoutAttributes *)attr {
  59. if(attr.representedElementKind != nil) {
  60. return;
  61. }
  62. // 获取当前item的大小
  63. CGFloat itemW = self.itemSize.width;
  64. CGFloat itemH = self.itemSize.height;
  65. // 获取当前section的item数量
  66. NSInteger itemCount = [self.collectionView numberOfItemsInSection:attr.indexPath.section];
  67. // 获取横排item数量
  68. CGFloat width = self.collectionView.bounds.size.width;
  69. // 获取行间距和item最小间距
  70. CGFloat lineDis = self.minimumLineSpacing;
  71. CGFloat itemDis = self.minimumInteritemSpacing;
  72. // 获取当前item的索引index
  73. NSInteger index = attr.indexPath.item;
  74. // 获取每页item数量
  75. NSInteger allCount = self.rowCount * self.columCount;
  76. // 获取item在当前section的页码
  77. NSInteger page = index / allCount;
  78. // 获取item x y方向偏移量
  79. NSInteger xIndex = index % self.rowCount;
  80. NSInteger yIndex = (index - page * allCount)/self.rowCount;
  81. // 获取x y方向偏移距离
  82. CGFloat xOffset = xIndex * (itemW + lineDis) + self.sectionInset.left;
  83. CGFloat yOffset = yIndex * (itemH + itemDis) + self.sectionInset.top;
  84. // 获取每个item占了几页
  85. NSInteger sectionPage = (itemCount % allCount == 0) ? itemCount/allCount : (itemCount/allCount + 1);
  86. // 保存每个section的page数量
  87. [self.pageDict setObject:@(sectionPage) forKey:[NSString stringWithFormat:@"%lu",attr.indexPath.section]];
  88. // 将所有section页面数量相加
  89. NSInteger allPagesCount = 0;
  90. for (NSString *page in [self.pageDict allKeys]) {
  91. allPagesCount += allPagesCount + [self.pageDict[page] integerValue];
  92. }
  93. // 获取到的数减去最后一页的页码数
  94. NSInteger lastIndex = self.pageDict.allKeys.count - 1;
  95. allPagesCount -= [self.pageDict[[NSString stringWithFormat:@"%lu",lastIndex]] integerValue];
  96. xOffset += page * width + allPagesCount * width;
  97. attr.frame = CGRectMake(xOffset, yOffset, itemW, itemH);
  98. }
  99. #pragma mark - getter and setter
  100. - (NSMutableArray *)attrs {
  101. if (!_attrs) {
  102. _attrs = [NSMutableArray array];
  103. }
  104. return _attrs;
  105. }
  106. - (NSMutableDictionary *)pageDict {
  107. if (!_pageDict) {
  108. _pageDict = [NSMutableDictionary dictionary];
  109. }
  110. return _pageDict;
  111. }
  112. @end