2
0

XLPageBasicTitleView.m 16 KB

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