CGXVerticalMenuContainerView.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // CGXVerticalMenuContainerView.m
  3. // CGXVerticalMenuView-OC
  4. //
  5. // Created by CGX on 2018/05/01.
  6. // Copyright © 2019 CGX. All rights reserved.
  7. //
  8. #import "CGXVerticalMenuContainerView.h"
  9. @interface CGXVerticalMenuContainerView() <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
  10. @property (nonatomic, strong, readwrite) CGXVerticalMenuContainerCollectionView *collectionView;
  11. @property (nonatomic , assign) NSInteger currentInteger;
  12. @property (nonatomic, assign) BOOL isFirstLayoutSubviews;
  13. @end
  14. @implementation CGXVerticalMenuContainerView
  15. - (instancetype)initWithFrame:(CGRect)frame
  16. {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. [self initializeViews];
  20. }
  21. return self;
  22. }
  23. - (void)initializeViews
  24. {
  25. self.animated = NO;
  26. self.isClickScroll = YES;
  27. self.currentInteger = 0;
  28. self.spaceLeft = 0;
  29. self.spaceRight = 0;
  30. self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
  31. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  32. layout.minimumLineSpacing = 0;
  33. layout.minimumInteritemSpacing = 0;
  34. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  35. _collectionView = [[CGXVerticalMenuContainerCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  36. self.collectionView.showsHorizontalScrollIndicator = NO;
  37. self.collectionView.showsVerticalScrollIndicator = NO;
  38. self.collectionView.pagingEnabled = YES;
  39. self.collectionView.scrollsToTop = NO;
  40. self.collectionView.scrollEnabled = NO;
  41. self.collectionView.bounces = NO;
  42. self.collectionView.delegate = self;
  43. self.collectionView.dataSource = self;
  44. self.collectionView.backgroundColor = self.backgroundColor;
  45. [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
  46. if (@available(iOS 10.0, *)) {
  47. self.collectionView.prefetchingEnabled = NO;
  48. }
  49. if (@available(iOS 11.0, *)) {
  50. self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  51. }
  52. [self addSubview:self.collectionView];
  53. }
  54. - (void)layoutSubviews {
  55. [super layoutSubviews];
  56. self.collectionView.frame = self.bounds;
  57. if (!CGRectEqualToRect(self.collectionView.frame, self.bounds)) {
  58. self.collectionView.frame = self.bounds;
  59. [self.collectionView.collectionViewLayout invalidateLayout];
  60. [self.collectionView reloadData];
  61. }
  62. if (self.currentInteger >=0 && [self.dataSouce numberOfRowsInListContainerView:self] >= self.currentInteger + 1) {
  63. [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.currentInteger inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  64. }
  65. if (self.isFirstLayoutSubviews) {
  66. self.isFirstLayoutSubviews = NO;
  67. [self.collectionView setContentOffset:CGPointMake(0, self.collectionView.bounds.size.height*self.currentInteger) animated:NO];
  68. }
  69. }
  70. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  71. {
  72. return 1;
  73. }
  74. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  75. if (self.dataSouce == nil) {
  76. return 0;
  77. }
  78. return [self.dataSouce numberOfRowsInListContainerView:self];
  79. }
  80. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  81. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  82. for (UIView *view in cell.contentView.subviews) {
  83. [view removeFromSuperview];
  84. }
  85. UIView *listView = [self.dataSouce verticalListContainerView:self listViewInRow:indexPath.item];
  86. listView.frame = CGRectMake(self.spaceLeft, 0, cell.contentView.bounds.size.width-self.spaceLeft-self.spaceRight, cell.contentView.bounds.size.height);
  87. [cell.contentView addSubview:listView];
  88. return cell;
  89. }
  90. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  91. if (self.delegate && [self.delegate respondsToSelector:@selector(verticalListContainerView:willDisplayCellAtRow:)]) {
  92. [self.delegate verticalListContainerView:self willDisplayCellAtRow:indexPath.item];
  93. }
  94. }
  95. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  96. if (self.delegate && [self.delegate respondsToSelector:@selector(verticalListContainerView:didEndDisplayingCellAtRow:)]) {
  97. [self.delegate verticalListContainerView:self didEndDisplayingCellAtRow:indexPath.item];
  98. }
  99. }
  100. - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
  101. return false;
  102. }
  103. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  104. return self.bounds.size;
  105. }
  106. - (void)reloadDataToItemAtIndex:(NSInteger)integer
  107. {
  108. [self.collectionView reloadData];
  109. if (self.dataSouce == nil) {
  110. return;
  111. }
  112. if (integer >= [self.dataSouce numberOfRowsInListContainerView:self]) {
  113. return;
  114. }
  115. self.currentInteger = integer;
  116. [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.currentInteger inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:(self.isClickScroll ? NO:self.animated)];
  117. [self.collectionView reloadData];
  118. }
  119. - (void)reloadData
  120. {
  121. [self.collectionView reloadData];
  122. }
  123. /*
  124. // Only override drawRect: if you perform custom drawing.
  125. // An empty implementation adversely affects performance during animation.
  126. - (void)drawRect:(CGRect)rect {
  127. // Drawing code
  128. }
  129. */
  130. @end