CGXVerticalMenuCategoryView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // CGXVerticalMenuView.m
  3. // CGXVerticalMenuView-OC
  4. //
  5. // Created by CGX on 2018/05/01.
  6. // Copyright © 2019 CGX. All rights reserved.
  7. //
  8. #import "CGXVerticalMenuCategoryView.h"
  9. typedef NS_ENUM(NSUInteger, CGXVerticalMenuCategoryViewDropUpDownType) {
  10. CGXVerticalMenuCategoryViewDropUpDownNodel,
  11. CGXVerticalMenuCategoryViewDropUpDownTop,
  12. CGXVerticalMenuCategoryViewDropUpDownBottom
  13. };
  14. @interface CGXVerticalMenuCategoryView ()<CGXVerticalMenuTitleViewDelegate,CGXVerticalMenuCollectionViewDelegate,CGXVerticalMenuCollectionViewDataSouce,CGXVerticalMenuContainerViewDataSouce,CGXVerticalMenuContainerViewDelegate>
  15. @property (nonatomic , strong,readwrite) NSMutableArray <CGXVerticalMenuCategoryListModel *> *dataArray;
  16. @property (nonatomic , assign,readwrite) NSInteger currentInteger;
  17. @property (nonatomic , assign) CGXVerticalMenuCategoryViewDropUpDownType dropUpDown;
  18. @property (nonatomic , strong) CGXVerticalMenuTitleView *leftView;
  19. @property (nonatomic , strong) CGXVerticalMenuContainerView *containerView;
  20. // 是否是点击滚动翻页的,为YES时 和 animated无效,无滚动动画
  21. @property (nonatomic, assign) BOOL isClickScroll;
  22. @end
  23. @implementation CGXVerticalMenuCategoryView
  24. - (void)initializeData
  25. {
  26. [super initializeData];
  27. self.leftBgColor = [UIColor colorWithWhite:0.93 alpha:1];;
  28. self.rightBgColor = [UIColor whiteColor];
  29. self.dropUpDown = CGXVerticalMenuCategoryViewDropUpDownNodel;
  30. self.titleWidth = 100;
  31. self.currentInteger = 0;
  32. self.dataArray = [NSMutableArray array];
  33. self.spaceLeft = 0;
  34. self.spaceRight = 0;
  35. }
  36. - (void)initializeViews
  37. {
  38. [super initializeViews];
  39. self.leftView = [[CGXVerticalMenuTitleView alloc] initWithFrame:CGRectMake(0, 0, self.titleWidth, CGRectGetHeight(self.bounds))];
  40. self.leftView.delegate = self;
  41. self.leftView.backgroundColor = self.leftBgColor;
  42. [self addSubview:self.leftView];
  43. self.containerView = [[CGXVerticalMenuContainerView alloc] initWithFrame:CGRectMake(self.titleWidth, 0, CGRectGetWidth(self.bounds)-self.titleWidth, CGRectGetHeight(self.bounds))];
  44. self.containerView.backgroundColor = self.rightBgColor;
  45. self.containerView.dataSouce = self;
  46. self.containerView.delegate = self;
  47. [self addSubview:self.containerView];
  48. self.containerView.animated = self.scrollAnimated;
  49. CGXVerticalMenuIndicatorLineView *lineView = [[CGXVerticalMenuIndicatorLineView alloc] init];
  50. lineView.backgroundColor = [UIColor redColor];
  51. lineView.positionType = CGXVerticalMenuIndicatorLinePosition_Left;
  52. self.leftView.indicators = @[lineView];
  53. }
  54. - (void)layoutSubviews
  55. {
  56. [super layoutSubviews];
  57. self.leftView.frame = CGRectMake(0, 0, self.titleWidth, CGRectGetHeight(self.bounds));
  58. self.containerView.frame = CGRectMake(self.titleWidth, 0, CGRectGetWidth(self.bounds)-self.titleWidth, CGRectGetHeight(self.bounds));
  59. }
  60. - (void)setSpaceLeft:(CGFloat)spaceLeft
  61. {
  62. _spaceLeft = spaceLeft;
  63. self.containerView.spaceLeft = spaceLeft;
  64. }
  65. - (void)setSpaceRight:(CGFloat)spaceRight
  66. {
  67. _spaceRight = spaceRight;
  68. self.containerView.spaceRight = spaceRight;
  69. }
  70. - (void)setLeftBgColor:(UIColor *)leftBgColor
  71. {
  72. _leftBgColor = leftBgColor;
  73. self.leftView.backgroundColor = leftBgColor;
  74. }
  75. - (void)setRightBgColor:(UIColor *)rightBgColor
  76. {
  77. _rightBgColor = rightBgColor;
  78. self.containerView.backgroundColor = rightBgColor;
  79. }
  80. - (void)setIndicators:(NSArray<UIView<CGXCategoryListIndicatorProtocol> *> *)indicators
  81. {
  82. _indicators = indicators;
  83. self.leftView.indicators = indicators;
  84. }
  85. - (void)updateListWithDataArray:(NSMutableArray<CGXVerticalMenuCategoryListModel *> *)dataArray
  86. {
  87. [self.dataArray removeAllObjects];
  88. [self.dataArray addObjectsFromArray:dataArray];
  89. NSMutableArray *dataArr = [NSMutableArray array];
  90. for (CGXVerticalMenuCategoryListModel *listModel in self.dataArray) {
  91. CGXVerticalMenuTitleModel *itemModel = listModel.leftModel;
  92. [dataArr addObject:itemModel];
  93. }
  94. [self.leftView updateMenuWithDataArray:dataArr];
  95. }
  96. /*
  97. 更新某个下标数据使用
  98. */
  99. - (void)updateListWistAtIndex:(NSInteger)index ItemModel:(CGXVerticalMenuCategoryListModel *)itemModel
  100. {
  101. if (self.dataArray.count==0 || index<0) {
  102. return;
  103. }
  104. if (index>self.dataArray.count-1) {
  105. return;
  106. }
  107. [self.dataArray replaceObjectAtIndex:index withObject:itemModel];
  108. [self.containerView reloadDataToItemAtIndex:index];
  109. }
  110. - (void)reloadData
  111. {
  112. [self.containerView reloadData];
  113. }
  114. /**
  115. 选中目标index的item
  116. @param index 目标index
  117. */
  118. - (void)selectItemAtIndex:(NSInteger)index
  119. {
  120. self.currentInteger = index;
  121. [self.leftView selectItemAtIndex:index];
  122. }
  123. - (void)refreshLoadData
  124. {
  125. if (self.currentInteger > 0) {
  126. self.currentInteger = self.currentInteger-1;
  127. }
  128. self.currentInteger = self.currentInteger;
  129. [self.leftView scrollerItemAtIndex:self.currentInteger];
  130. }
  131. - (void)refreshMoreLoadData
  132. {
  133. if (self.currentInteger < self.dataArray.count-1) {
  134. self.currentInteger = self.currentInteger+1;
  135. }
  136. self.currentInteger = self.currentInteger;
  137. [self.leftView scrollerItemAtIndex:self.currentInteger];
  138. }
  139. - (void)verticalMenuTitleView:(CGXVerticalMenuTitleView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index
  140. {
  141. self.currentInteger = index;
  142. self.isClickScroll = YES;
  143. }
  144. - (void)verticalMenuTitleView:(CGXVerticalMenuTitleView *)categoryView didScrollerSelectedItemAtIndex:(NSInteger)index
  145. {
  146. self.currentInteger = index;
  147. self.isClickScroll = NO;
  148. }
  149. - (void)verticalMenuTitleView:(CGXVerticalMenuTitleView *)categoryView didSelectedItemAtIndex:(NSInteger)index
  150. {
  151. self.currentInteger = index;
  152. self.containerView.isClickScroll = self.isClickScroll;
  153. self.containerView.animated = self.scrollAnimated;
  154. [self.containerView reloadDataToItemAtIndex:index];
  155. if (self.delegate && [self.delegate respondsToSelector:@selector(verticalMenuView:didSelectedItemAtIndex:)]) {
  156. [self.delegate verticalMenuView:self didSelectedItemAtIndex:index];
  157. }
  158. }
  159. - (NSInteger)numberOfRowsInListContainerView:(CGXVerticalMenuContainerView *)listContainerView
  160. {
  161. return self.dataArray.count;
  162. }
  163. - (UIView *)verticalListContainerView:(CGXVerticalMenuContainerView *)listContainerView listViewInRow:(NSInteger)row
  164. {
  165. CGXVerticalMenuCategoryListModel *listModel = self.dataArray[row];
  166. CGXVerticalMenuCollectionView *rightView = [[CGXVerticalMenuCollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
  167. rightView.delegate = self;
  168. rightView.dataSouce = self;
  169. [rightView updateRightWithDataArray:listModel.rightArray];
  170. if (self.delegate && [self.delegate respondsToSelector:@selector(verticalMenuView:RefreshScrollView:listViewInRow:)]) {
  171. [self.delegate verticalMenuView:self RefreshScrollView:rightView.collectionView listViewInRow:row];
  172. }
  173. return rightView;
  174. }
  175. - (void)categoryRightView:(CGXVerticalMenuCollectionView *)categoryView didClickSelectedItemAtIndexPath:(NSIndexPath *)indexPath
  176. {
  177. if (self.delegate && [self.delegate respondsToSelector:@selector(verticalMenuView:didSelectedItemDetailsAtIndexPath:)]) {
  178. [self.delegate verticalMenuView:self didSelectedItemDetailsAtIndexPath:indexPath];
  179. }
  180. }
  181. - (void)categoryRightView:(CGXVerticalMenuCollectionView *)categoryView didSelectDecorationViewAtIndexPath:(NSIndexPath *)indexPath
  182. {
  183. if (self.delegate && [self.delegate respondsToSelector:@selector(verticalMenuView:didSelectDecorationViewAtIndexPath:)]) {
  184. [self.delegate verticalMenuView:self didSelectDecorationViewAtIndexPath:indexPath];
  185. }
  186. }
  187. - (void)verticalListContainerView:(CGXVerticalMenuContainerView *)listContainerView willDisplayCellAtRow:(NSInteger)row
  188. {
  189. if (self.delegate && [self.delegate respondsToSelector:@selector(verticalMenuView:willDisplayCellAtRow:)]) {
  190. [self.delegate verticalMenuView:self willDisplayCellAtRow:row];
  191. }
  192. }
  193. - (void)verticalListContainerView:(CGXVerticalMenuContainerView *)listContainerView didEndDisplayingCellAtRow:(NSInteger)row
  194. {
  195. if (self.delegate && [self.delegate respondsToSelector:@selector(verticalMenuView:didEndDisplayingCellAtRow:)]) {
  196. [self.delegate verticalMenuView:self didEndDisplayingCellAtRow:row];
  197. }
  198. }
  199. /** 如果你需要自定义cell样式,请在实现此代理方法返回你的自定义cell的class。 */
  200. - (Class)customcategoryRightViewCollectionViewCellClass
  201. {
  202. if (self.dataSouce && [self.dataSouce respondsToSelector:@selector(verticalMenuViewCustomCollectionViewCellClass)] && [self.dataSouce verticalMenuViewCustomCollectionViewCellClass]) {
  203. return [self.dataSouce verticalMenuViewCustomCollectionViewCellClass];
  204. }
  205. return nil;
  206. }
  207. /** 如果你需要自定义cell样式,请在实现此代理方法返回你的自定义cell的Nib。 */
  208. - (Class)customcategoryRightViewCollectionViewCellNib
  209. {
  210. if (self.dataSouce && [self.dataSouce respondsToSelector:@selector(verticalMenuViewCustomCollectionViewCellNib)] && [self.dataSouce verticalMenuViewCustomCollectionViewCellNib]) {
  211. return [self.dataSouce verticalMenuViewCustomCollectionViewCellNib];
  212. }
  213. return nil;
  214. }
  215. - (UICollectionViewCell *)categoryRightView:(CGXVerticalMenuCollectionView *)categoryView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  216. {
  217. if (self.dataSouce && [self.dataSouce respondsToSelector:@selector(verticalMenuView:ListView:cellForItemAtIndexPath:listViewInRow:)] && [self.dataSouce verticalMenuView:self ListView:categoryView cellForItemAtIndexPath:indexPath listViewInRow:self.currentInteger]) {
  218. return [self.dataSouce verticalMenuView:self ListView:categoryView cellForItemAtIndexPath:indexPath listViewInRow:self.currentInteger];
  219. }
  220. return nil;
  221. }
  222. - (UICollectionReusableView *)categoryRightView:(CGXVerticalMenuCollectionView *)categoryView KindHeadAtIndexPath:(NSIndexPath *)indexPath
  223. {
  224. if (self.dataSouce && [self.dataSouce respondsToSelector:@selector(verticalMenuView:ListView:KindHeadAtIndexPath:listViewInRow:)] && [self.dataSouce verticalMenuView:self ListView:categoryView KindHeadAtIndexPath:indexPath listViewInRow:self.currentInteger]) {
  225. return [self.dataSouce verticalMenuView:self ListView:categoryView KindHeadAtIndexPath:indexPath listViewInRow:self.currentInteger];
  226. }
  227. return nil;
  228. }
  229. - (UICollectionReusableView *)categoryRightView:(CGXVerticalMenuCollectionView *)categoryView KindFootAtIndexPath:(NSIndexPath *)indexPath
  230. {
  231. if (self.dataSouce && [self.dataSouce respondsToSelector:@selector(verticalMenuView:ListView:KindFootAtIndexPath:listViewInRow:)] && [self.dataSouce verticalMenuView:self ListView:categoryView KindFootAtIndexPath:indexPath listViewInRow:self.currentInteger]) {
  232. return [self.dataSouce verticalMenuView:self ListView:categoryView KindFootAtIndexPath:indexPath listViewInRow:self.currentInteger];
  233. }
  234. return nil;
  235. }
  236. /**
  237. 每个分区背景颜色 默认背景色
  238. */
  239. - (UIColor *)categoryRightView:(CGXVerticalMenuCollectionView *)categoryView BackgroundColorForSection:(NSInteger)section
  240. {
  241. if (self.dataSouce && [self.dataSouce respondsToSelector:@selector(verticalMenuView:BackgroundColorForSection:)]) {
  242. return [self.dataSouce verticalMenuView:self BackgroundColorForSection:section];
  243. }
  244. CGXVerticalMenuCollectionSectionModel *sectionModel = categoryView.dataArray[section];
  245. return sectionModel.sectionColor;
  246. }
  247. - (CGFloat)categoryRightView:(CGXVerticalMenuCollectionView *)categoryView sizeForItemAtSection:(NSInteger)section ItemWidth:(CGFloat)itemWidth
  248. {
  249. CGFloat height = itemWidth;
  250. if (self.dataSouce && [self.dataSouce respondsToSelector:@selector(verticalMenuView:sizeForItemAtSection:ItemWidth:)]) {
  251. height = [self.dataSouce verticalMenuView:self sizeForItemAtSection:section ItemWidth:itemWidth];
  252. }
  253. return height;
  254. }
  255. // CollectionView分区标题即将展示
  256. - (void)categoryRightView:(CGXVerticalMenuCollectionView *)categoryView willDisplaySupplementaryView:(UICollectionReusableView *)view
  257. forElementKind:(NSString *)elementKind
  258. atIndexPath:(NSIndexPath *)indexPath
  259. {
  260. if (self.delegate && [self.delegate respondsToSelector:@selector(verticalMenuView:willDisplaViewElementKind:atIndexPath:)]) {
  261. [self.delegate verticalMenuView:self willDisplaViewElementKind:elementKind atIndexPath:indexPath];
  262. }
  263. }
  264. // CollectionView分区标题展示结束
  265. - (void)categoryRightView:(CGXVerticalMenuCollectionView *)categoryView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view
  266. forElementOfKind:(NSString *)elementKind
  267. atIndexPath:(NSIndexPath *)indexPath
  268. {
  269. if (self.delegate && [self.delegate respondsToSelector:@selector(verticalMenuView:didEndDisplayingElementKind:atIndexPath:)]) {
  270. [self.delegate verticalMenuView:self didEndDisplayingElementKind:elementKind atIndexPath:indexPath];
  271. }
  272. }
  273. @end