JXCategoryBaseView.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // JXCategoryView.h
  3. // UI系列测试
  4. //
  5. // Created by jiaxin on 2018/3/15.
  6. // Copyright © 2018年 jiaxin. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import "JXCategoryBaseCell.h"
  10. #import "JXCategoryBaseCellModel.h"
  11. #import "JXCategoryCollectionView.h"
  12. #import "JXCategoryViewDefines.h"
  13. @class JXCategoryBaseView;
  14. @protocol JXCategoryViewListContainer <NSObject>
  15. - (void)setDefaultSelectedIndex:(NSInteger)index;
  16. - (UIScrollView *)contentScrollView;
  17. - (void)reloadData;
  18. - (void)didClickSelectedItemAtIndex:(NSInteger)index;
  19. @end
  20. @protocol JXCategoryViewDelegate <NSObject>
  21. @optional
  22. //为什么会把选中代理分为三个,因为有时候只关心点击选中的,有时候只关心滚动选中的,有时候只关心选中。所以具体情况,使用对应方法。
  23. /**
  24. 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件,不关心具体是点击还是滚动选中的。
  25. @param categoryView categoryView对象
  26. @param index 选中的index
  27. */
  28. - (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index;
  29. /**
  30. 点击选中的情况才会调用该方法
  31. @param categoryView categoryView对象
  32. @param index 选中的index
  33. */
  34. - (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index;
  35. /**
  36. 滚动选中的情况才会调用该方法
  37. @param categoryView categoryView对象
  38. @param index 选中的index
  39. */
  40. - (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index;
  41. /**
  42. 控制能否点击Item
  43. @param categoryView categoryView对象
  44. @param index 准备点击的index
  45. @return 能否点击
  46. */
  47. - (BOOL)categoryView:(JXCategoryBaseView *)categoryView canClickItemAtIndex:(NSInteger)index;
  48. /**
  49. 正在滚动中的回调
  50. @param categoryView categoryView对象
  51. @param leftIndex 正在滚动中,相对位置处于左边的index
  52. @param rightIndex 正在滚动中,相对位置处于右边的index
  53. @param ratio 从左往右计算的百分比
  54. */
  55. - (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio;
  56. @end
  57. @interface JXCategoryBaseView : UIView
  58. @property (nonatomic, strong, readonly) JXCategoryCollectionView *collectionView;
  59. @property (nonatomic, strong) NSArray <JXCategoryBaseCellModel *> *dataSource;
  60. @property (nonatomic, weak) id<JXCategoryViewDelegate> delegate;
  61. /**
  62. 高封装度的列表容器,使用该类可以让列表拥有完成的生命周期、自动同步defaultSelectedIndex、自动调用reloadData。
  63. */
  64. @property (nonatomic, weak) id<JXCategoryViewListContainer> listContainer;
  65. /**
  66. 推荐使用封装度更高的listContainer属性。如果使用contentScrollView请参考`LoadDataListCustomViewController`使用示例。
  67. */
  68. @property (nonatomic, strong) UIScrollView *contentScrollView;
  69. @property (nonatomic, assign) NSInteger defaultSelectedIndex; //修改初始化的时候默认选择的index
  70. @property (nonatomic, assign, readonly) NSInteger selectedIndex;
  71. @property (nonatomic, assign, getter=isContentScrollViewClickTransitionAnimationEnabled) BOOL contentScrollViewClickTransitionAnimationEnabled; //点击cell进行contentScrollView切换时是否需要动画。默认为YES
  72. @property (nonatomic, assign) CGFloat contentEdgeInsetLeft; //整体内容的左边距,默认JXCategoryViewAutomaticDimension(等于cellSpacing)
  73. @property (nonatomic, assign) CGFloat contentEdgeInsetRight; //整体内容的右边距,默认JXCategoryViewAutomaticDimension(等于cellSpacing)
  74. @property (nonatomic, assign) CGFloat cellWidth; //默认JXCategoryViewAutomaticDimension
  75. @property (nonatomic, assign) CGFloat cellWidthIncrement; //cell宽度补偿。默认:0
  76. @property (nonatomic, assign) CGFloat cellSpacing; //cell之间的间距,默认20
  77. @property (nonatomic, assign, getter=isAverageCellSpacingEnabled) BOOL averageCellSpacingEnabled; //当collectionView.contentSize.width小于JXCategoryBaseView的宽度,是否将cellSpacing均分。默认为YES。
  78. //cell宽度是否缩放
  79. @property (nonatomic, assign, getter=isCellWidthZoomEnabled) BOOL cellWidthZoomEnabled; //默认为NO
  80. @property (nonatomic, assign, getter=isCellWidthZoomScrollGradientEnabled) BOOL cellWidthZoomScrollGradientEnabled; //手势滚动过程中,是否需要更新cell的宽度。默认为YES
  81. @property (nonatomic, assign) CGFloat cellWidthZoomScale; //默认1.2,cellWidthZoomEnabled为YES才生效
  82. @property (nonatomic, assign, getter=isSelectedAnimationEnabled) BOOL selectedAnimationEnabled; //是否开启点击或代码选中动画。默认为NO。自定义的cell选中动画需要自己实现。(仅点击或调用selectItemAtIndex选中才有效,滚动选中无效)
  83. @property (nonatomic, assign) NSTimeInterval selectedAnimationDuration; //cell选中动画的时间。默认0.25
  84. /**
  85. 选中目标index的item
  86. @param index 目标index
  87. */
  88. - (void)selectItemAtIndex:(NSInteger)index;
  89. /**
  90. 初始化的时候无需调用。比如页面初始化之后,根据网络接口异步回调回来数据,重新配置categoryView,需要调用该方法进行刷新。
  91. */
  92. - (void)reloadData;
  93. /**
  94. 重新配置categoryView但是不需要reload listContainer。特殊情况是该方法。
  95. */
  96. - (void)reloadDataWithoutListContainer;
  97. /**
  98. 刷新指定的index的cell
  99. 内部会触发`- (void)refreshCellModel:(JXCategoryBaseCellModel *)cellModel index:(NSInteger)index`方法进行cellModel刷新
  100. @param index 指定cell的index
  101. */
  102. - (void)reloadCellAtIndex:(NSInteger)index;
  103. @end
  104. @interface JXCategoryBaseView (UISubclassingBaseHooks)
  105. /**
  106. 获取目标cell当前的frame,反应当前真实的frame受到cellWidthSelectedZoomScale的影响。
  107. */
  108. - (CGRect)getTargetCellFrame:(NSInteger)targetIndex;
  109. /**
  110. 获取目标cell的选中时的frame,其他cell的状态都当做普通状态处理。
  111. */
  112. - (CGRect)getTargetSelectedCellFrame:(NSInteger)targetIndex selectedType:(JXCategoryCellSelectedType)selectedType;
  113. - (void)initializeData NS_REQUIRES_SUPER;
  114. - (void)initializeViews NS_REQUIRES_SUPER;
  115. /**
  116. reloadData方法调用,重新生成数据源赋值到self.dataSource
  117. */
  118. - (void)refreshDataSource;
  119. /**
  120. reloadData方法调用,根据数据源重新刷新状态;
  121. */
  122. - (void)refreshState NS_REQUIRES_SUPER;
  123. /**
  124. 选中某个item时,刷新将要选中与取消选中的cellModel
  125. @param selectedCellModel 将要选中的cellModel
  126. @param unselectedCellModel 取消选中的cellModel
  127. */
  128. - (void)refreshSelectedCellModel:(JXCategoryBaseCellModel *)selectedCellModel unselectedCellModel:(JXCategoryBaseCellModel *)unselectedCellModel NS_REQUIRES_SUPER;
  129. /**
  130. 关联的contentScrollView的contentOffset发生了改变
  131. @param contentOffset 偏移量
  132. */
  133. - (void)contentOffsetOfContentScrollViewDidChanged:(CGPoint)contentOffset NS_REQUIRES_SUPER;
  134. /**
  135. 选中某一个item的时候调用,该方法用于子类重载。
  136. 如果外部要选中某个index,请使用`- (void)selectItemAtIndex:(NSUInteger)index;`
  137. @param index 选中的index
  138. @param selectedType JXCategoryCellSelectedType
  139. @return 返回值为NO,表示触发内部某些判断(点击了同一个cell),子类无需后续操作。
  140. */
  141. - (BOOL)selectCellAtIndex:(NSInteger)index selectedType:(JXCategoryCellSelectedType)selectedType NS_REQUIRES_SUPER;
  142. /**
  143. reloadData时,返回每个cell的宽度
  144. @param index 目标index
  145. @return cellWidth
  146. */
  147. - (CGFloat)preferredCellWidthAtIndex:(NSInteger)index;
  148. /**
  149. 返回自定义cell的class
  150. @return cell class
  151. */
  152. - (Class)preferredCellClass;
  153. /**
  154. refreshState时调用,重置cellModel的状态
  155. @param cellModel 待重置的cellModel
  156. @param index cellModel在数组中的index
  157. */
  158. - (void)refreshCellModel:(JXCategoryBaseCellModel *)cellModel index:(NSInteger)index NS_REQUIRES_SUPER;
  159. @end