酷店

XLPageBasicTitleView.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // XLPageBasicTitleView.m
  3. // XLPageViewControllerExample
  4. //
  5. // Created by MengXianLiang on 2019/5/8.
  6. // Copyright © 2019 jwzt. All rights reserved.
  7. // https://github.com/mengxianliang/XLPageViewController
  8. #import "XLPageBasicTitleView.h"
  9. #import "XLPageViewControllerUtil.h"
  10. #import "XLPageTitleCell.h"
  11. #pragma mark - 布局类
  12. #pragma mark XLPageBasicTitleViewFolowLayout
  13. @interface XLPageBasicTitleViewFolowLayout : UICollectionViewFlowLayout
  14. @property (nonatomic, assign) XLPageTitleViewAlignment alignment;
  15. @property (nonatomic, assign) UIEdgeInsets originSectionInset;
  16. @property (nonatomic, assign) BOOL haveUpdateInset;
  17. @end
  18. @implementation XLPageBasicTitleViewFolowLayout
  19. //设置标题居中、局左、居右方法
  20. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
  21. if (self.haveUpdateInset) {
  22. return [super layoutAttributesForElementsInRect:rect];
  23. }
  24. CGRect targetRect = rect;
  25. targetRect.size = self.collectionView.bounds.size;
  26. //获取屏幕上所有布局文件
  27. NSArray *attributes = [super layoutAttributesForElementsInRect:targetRect];
  28. //获取所有item个数
  29. CGFloat totalItemCount = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:0];
  30. //如果屏幕未被item充满,执行以下布局,否则保持标准布局
  31. if (attributes.count < totalItemCount) {
  32. return [super layoutAttributesForElementsInRect:rect];
  33. }
  34. self.haveUpdateInset = true;
  35. //获取第一个cell左边和最后一个cell右边之间的距离
  36. UICollectionViewLayoutAttributes *firstAttribute = attributes.firstObject;
  37. UICollectionViewLayoutAttributes *lastAttribute = attributes.lastObject;
  38. CGFloat attributesFullWidth = CGRectGetMaxX(lastAttribute.frame) - CGRectGetMinX(firstAttribute.frame);
  39. //计算留白宽度
  40. CGFloat emptyWidth = self.collectionView.bounds.size.width - attributesFullWidth;
  41. //设置左缩进
  42. CGFloat insetLeft = 0;
  43. if (self.alignment == XLPageTitleViewAlignmentLeft) {
  44. insetLeft = self.originSectionInset.left;
  45. }
  46. if (self.alignment == XLPageTitleViewAlignmentCenter) {
  47. insetLeft = emptyWidth/2.0f;
  48. }
  49. if (self.alignment == XLPageTitleViewAlignmentRight) {
  50. insetLeft = emptyWidth - self.originSectionInset.right;
  51. }
  52. //兼容防止出错,最小缩进设置为原始缩进
  53. insetLeft = insetLeft <= self.originSectionInset.left ? self.originSectionInset.left : insetLeft;
  54. //如果和当前缩进一直,则不需要更新缩进
  55. if (insetLeft == self.sectionInset.left) {
  56. return [super layoutAttributesForElementsInRect:rect];
  57. }
  58. //更新CollectionView缩进
  59. self.sectionInset = UIEdgeInsetsMake(self.sectionInset.top, insetLeft, self.sectionInset.bottom, self.sectionInset.right);
  60. //返回
  61. return [super layoutAttributesForElementsInRect:rect];
  62. }
  63. @end
  64. #pragma mark - 标题类
  65. #pragma mark XLPageBasicTitleView
  66. @interface XLPageBasicTitleView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  67. //布局
  68. @property (nonatomic, strong) XLPageBasicTitleViewFolowLayout *layout;
  69. //集合视图
  70. @property (nonatomic, strong) UICollectionView *collectionView;
  71. //配置信息
  72. @property (nonatomic, strong) XLPageViewControllerConfig *config;
  73. //阴影线条
  74. @property (nonatomic, strong) UIView *shadowLine;
  75. //底部分割线
  76. @property (nonatomic, strong) UIView *separatorLine;
  77. @end
  78. @implementation XLPageBasicTitleView
  79. - (instancetype)initWithConfig:(XLPageViewControllerConfig *)config {
  80. if (self = [super init]) {
  81. [self initTitleViewWithConfig:config];
  82. }
  83. return self;
  84. }
  85. - (void)initTitleViewWithConfig:(XLPageViewControllerConfig *)config {
  86. self.config = config;
  87. self.layout = [[XLPageBasicTitleViewFolowLayout alloc] init];
  88. self.layout.alignment = self.config.titleViewAlignment;
  89. self.layout.originSectionInset = self.config.titleViewInset;
  90. self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  91. self.layout.sectionInset = config.titleViewInset;
  92. self.layout.minimumInteritemSpacing = config.titleSpace;
  93. self.layout.minimumLineSpacing = config.titleSpace;
  94. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
  95. self.collectionView.delegate = self;
  96. self.collectionView.dataSource = self;
  97. self.collectionView.backgroundColor = config.titleViewBackgroundColor;
  98. [self.collectionView registerClass:[XLPageTitleCell class] forCellWithReuseIdentifier:@"XLPageTitleCell"];
  99. self.collectionView.showsHorizontalScrollIndicator = false;
  100. [self addSubview:self.collectionView];
  101. self.separatorLine = [[UIView alloc] init];
  102. self.separatorLine.backgroundColor = config.separatorLineColor;
  103. self.separatorLine.hidden = config.separatorLineHidden;
  104. [self addSubview:self.separatorLine];
  105. self.shadowLine = [[UIView alloc] init];
  106. self.shadowLine.bounds = CGRectMake(0, 0, self.config.shadowLineWidth, self.config.shadowLineHeight);
  107. self.shadowLine.backgroundColor = config.shadowLineColor;
  108. self.shadowLine.layer.cornerRadius = self.config.shadowLineHeight/2.0f;
  109. if (self.config.shadowLineCap == XLPageShadowLineCapSquare) {
  110. self.shadowLine.layer.cornerRadius = 0;
  111. }
  112. self.shadowLine.layer.masksToBounds = true;
  113. self.shadowLine.hidden = config.shadowLineHidden;
  114. [self.collectionView addSubview:self.shadowLine];
  115. self.stopAnimation = false;
  116. }
  117. - (void)layoutSubviews {
  118. [super layoutSubviews];
  119. CGFloat collectionW = self.bounds.size.width;
  120. if (self.rightButton) {
  121. CGFloat btnW = self.bounds.size.height;
  122. collectionW = self.bounds.size.width - btnW;
  123. self.rightButton.frame = CGRectMake(self.bounds.size.width - btnW, 0, btnW, btnW);
  124. }
  125. self.collectionView.frame = CGRectMake(0, 0, collectionW, self.bounds.size.height);
  126. self.separatorLine.frame = CGRectMake(0, self.bounds.size.height - self.config.separatorLineHeight, self.bounds.size.width, self.config.separatorLineHeight);
  127. self.shadowLine.center = [self shadowLineCenterForIndex:_selectedIndex];
  128. [self fixShadowLineCenter];
  129. [self.collectionView sendSubviewToBack:self.shadowLine];
  130. [self bringSubviewToFront:self.separatorLine];
  131. }
  132. #pragma mark -
  133. #pragma mark CollectionViewDataSource
  134. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  135. return [self.dataSource pageTitleViewNumberOfTitle];
  136. }
  137. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  138. return CGSizeMake([self widthForItemAtIndexPath:indexPath], collectionView.bounds.size.height - self.config.titleViewInset.top - self.config.titleViewInset.bottom);
  139. }
  140. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  141. XLPageTitleCell *cell = [self.dataSource pageTitleViewCellForItemAtIndex:indexPath.row];
  142. if (!cell) {
  143. cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XLPageTitleCell" forIndexPath:indexPath];
  144. }
  145. cell.config = self.config;
  146. cell.textLabel.text = [self.dataSource pageTitleViewTitleForIndex:indexPath.row];
  147. [cell configCellOfSelected:(indexPath.row == self.selectedIndex)];
  148. return cell;
  149. }
  150. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  151. BOOL switchSuccess = [self.delegate pageTitleViewDidSelectedAtIndex:indexPath.row];
  152. if (!switchSuccess) {return;}
  153. self.selectedIndex = indexPath.row;
  154. }
  155. #pragma mark -
  156. #pragma mark Setter
  157. - (void)setSelectedIndex:(NSInteger)selectedIndex {
  158. _selectedIndex = selectedIndex;
  159. [self updateLayout];
  160. }
  161. - (void)setRightButton:(UIButton *)rightButton {
  162. _rightButton = rightButton;
  163. [self addSubview:rightButton];
  164. }
  165. - (void)updateLayout {
  166. if (_selectedIndex == _lastSelectedIndex) {return;}
  167. //更新cellUI
  168. NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:_selectedIndex inSection:0];
  169. NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:_lastSelectedIndex inSection:0];
  170. [UIView performWithoutAnimation:^{
  171. [self.collectionView reloadItemsAtIndexPaths:@[indexPath1,indexPath2]];
  172. }];
  173. //自动居中
  174. [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true];
  175. //设置阴影位置
  176. self.shadowLine.center = [self shadowLineCenterForIndex:_selectedIndex];
  177. //保存上次选中位置
  178. _lastSelectedIndex = _selectedIndex;
  179. }
  180. - (void)fixShadowLineCenter {
  181. if (self.config.titleViewStyle == XLPageTitleViewStyleSegmented) {return;}
  182. //避免cell不在屏幕上显示,延时0.01秒加载
  183. if (self.shadowLine.center.x <= 0) {
  184. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
  185. self.shadowLine.center = [self shadowLineCenterForIndex:self.selectedIndex];
  186. });
  187. }
  188. }
  189. - (void)setAnimationProgress:(CGFloat)animationProgress {
  190. if (self.stopAnimation) {return;}
  191. if (animationProgress == 0) {return;}
  192. //获取下一个index
  193. NSInteger targetIndex = animationProgress < 0 ? _selectedIndex - 1 : _selectedIndex + 1;
  194. if (targetIndex < 0 || targetIndex >= [self.dataSource pageTitleViewNumberOfTitle]) {return;}
  195. //获取cell
  196. XLPageTitleCell *currentCell = (XLPageTitleCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0]];
  197. XLPageTitleCell *targetCell = (XLPageTitleCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:targetIndex inSection:0]];
  198. //标题颜色过渡
  199. if (self.config.titleColorTransition) {
  200. [currentCell showAnimationOfProgress:fabs(animationProgress) type:XLPageTitleCellAnimationTypeSelected];
  201. [targetCell showAnimationOfProgress:fabs(animationProgress) type:XLPageTitleCellAnimationTypeWillSelected];
  202. }
  203. //给阴影添加动画
  204. [XLPageViewControllerUtil showAnimationToShadow:self.shadowLine shadowWidth:self.config.shadowLineWidth fromItemRect:currentCell.frame toItemRect:targetCell.frame type:self.config.shadowLineAnimationType progress:animationProgress];
  205. }
  206. //刷新方法
  207. - (void)reloadData {
  208. self.layout.haveUpdateInset = false;
  209. [self.collectionView reloadData];
  210. }
  211. #pragma mark -
  212. #pragma mark 阴影位置
  213. - (CGPoint)shadowLineCenterForIndex:(NSInteger)index {
  214. XLPageTitleCell *cell = (XLPageTitleCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
  215. CGFloat centerX = cell.center.x;
  216. CGFloat separatorLineHeight = self.config.separatorLineHidden ? 0 : self.config.separatorLineHeight;
  217. CGFloat centerY = self.bounds.size.height - self.config.shadowLineHeight/2.0f - separatorLineHeight-self.config.separatorLineBottomMargin;
  218. if (self.config.shadowLineAlignment == XLPageShadowLineAlignmentTop) {
  219. centerY = self.config.shadowLineHeight/2.0f;
  220. }
  221. if (self.config.shadowLineAlignment == XLPageShadowLineAlignmentCenter) {
  222. centerY = cell.center.y;
  223. }
  224. return CGPointMake(centerX, centerY);
  225. }
  226. #pragma mark -
  227. #pragma mark 辅助方法
  228. - (CGFloat)widthForItemAtIndexPath:(NSIndexPath *)indexPath {
  229. if (self.config.titleWidth > 0) {
  230. return self.config.titleWidth;
  231. }
  232. CGFloat normalTitleWidth = [XLPageViewControllerUtil widthForText:[self.dataSource pageTitleViewTitleForIndex:indexPath.row] font:self.config.titleNormalFont size:self.bounds.size];
  233. CGFloat selectedTitleWidth = [XLPageViewControllerUtil widthForText:[self.dataSource pageTitleViewTitleForIndex:indexPath.row] font:self.config.titleSelectedFont size:self.bounds.size];
  234. return selectedTitleWidth > normalTitleWidth ? selectedTitleWidth : normalTitleWidth;
  235. }
  236. #pragma mark -
  237. #pragma mark 自定cell方法
  238. - (void)registerClass:(Class)cellClass forTitleViewCellWithReuseIdentifier:(NSString *)identifier {
  239. if (!identifier.length) {
  240. [NSException raise:@"This identifier must not be nil and must not be an empty string." format:@""];
  241. }
  242. if ([identifier isEqualToString:NSStringFromClass(XLPageTitleCell.class)]) {
  243. [NSException raise:@"please change an identifier" format:@""];
  244. }
  245. if (![cellClass isSubclassOfClass:[XLPageTitleCell class]]) {
  246. [NSException raise:@"The cell class must be a subclass of XLPageTitleCell." format:@""];
  247. }
  248. [self.collectionView registerClass:cellClass forCellWithReuseIdentifier:identifier];
  249. }
  250. - (__kindof XLPageTitleCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndex:(NSInteger)index {
  251. if (!identifier.length) {
  252. [NSException raise:@"This identifier must not be nil and must not be an empty string." format:@""];
  253. }
  254. if ([identifier isEqualToString:NSStringFromClass(XLPageTitleCell.class)]) {
  255. [NSException raise:@"please change an identifier" format:@""];
  256. }
  257. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
  258. if (!indexPath) {
  259. [NSException raise:@"please change an identifier" format:@""];
  260. }
  261. XLPageTitleCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
  262. return cell;
  263. }
  264. @end