JXCategoryBaseView.h 7.9 KB

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