Nenhuma Descrição

SGPageContentCollectionView.m 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // SGPageContentCollectionView.m
  3. // SGPagingViewExample
  4. //
  5. // Created by kingsic on 16/10/6.
  6. // Copyright © 2016年 kingsic. All rights reserved.
  7. //
  8. #import "SGPageContentCollectionView.h"
  9. #import "UIView+SGPagingView.h"
  10. @interface SGPageContentCollectionView () <UICollectionViewDataSource, UICollectionViewDelegate>
  11. /// 外界父控制器
  12. @property (nonatomic, weak) UIViewController *parentViewController;
  13. /// 存储子控制器
  14. @property (nonatomic, strong) NSArray *childViewControllers;
  15. /// collectionView
  16. @property (nonatomic, strong) UICollectionView *collectionView;
  17. /// 记录刚开始时的偏移量
  18. @property (nonatomic, assign) NSInteger startOffsetX;
  19. /// 记录加载的上个子控制器的下标
  20. @property (nonatomic, assign) NSInteger previousCVCIndex;
  21. /// 标记内容滚动
  22. @property (nonatomic, assign) BOOL isScroll;
  23. @end
  24. @implementation SGPageContentCollectionView
  25. static NSString *const cellID = @"cellID";
  26. - (instancetype)initWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs {
  27. if (self = [super initWithFrame:frame]) {
  28. if (parentVC == nil) {
  29. @throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageContentCollectionView 初始化方法中所在控制器必须设置" userInfo:nil];
  30. }
  31. self.parentViewController = parentVC;
  32. if (childVCs == nil) {
  33. @throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageContentCollectionView 初始化方法中子控制器必须设置" userInfo:nil];
  34. }
  35. self.childViewControllers = childVCs;
  36. [self initialization];
  37. [self setupSubviews];
  38. }
  39. return self;
  40. }
  41. + (instancetype)pageContentCollectionViewWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs {
  42. return [[self alloc] initWithFrame:frame parentVC:parentVC childVCs:childVCs];
  43. }
  44. - (void)initialization {
  45. _startOffsetX = 0;
  46. _previousCVCIndex = -1;
  47. }
  48. - (void)setupSubviews {
  49. // 0、处理偏移量
  50. UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero];
  51. [self addSubview:tempView];
  52. // 1、添加UICollectionView, 用于在Cell中存放控制器的View
  53. [self addSubview:self.collectionView];
  54. }
  55. - (UICollectionView *)collectionView {
  56. if (!_collectionView) {
  57. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  58. flowLayout.itemSize = self.bounds.size;
  59. flowLayout.minimumLineSpacing = 0;
  60. flowLayout.minimumInteritemSpacing = 0;
  61. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  62. CGFloat collectionViewX = 0;
  63. CGFloat collectionViewY = 0;
  64. CGFloat collectionViewW = self.SG_width;
  65. CGFloat collectionViewH = self.SG_height;
  66. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(collectionViewX, collectionViewY, collectionViewW, collectionViewH) collectionViewLayout:flowLayout];
  67. _collectionView.showsVerticalScrollIndicator = NO;
  68. _collectionView.showsHorizontalScrollIndicator = NO;
  69. _collectionView.pagingEnabled = YES;
  70. _collectionView.bounces = NO;
  71. _collectionView.backgroundColor = [UIColor whiteColor];
  72. _collectionView.delegate = self;
  73. _collectionView.dataSource = self;
  74. [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
  75. }
  76. return _collectionView;
  77. }
  78. #pragma mark - - - UICollectionViewDataSource
  79. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  80. return self.childViewControllers.count;
  81. }
  82. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  83. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  84. [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  85. // 设置内容
  86. UIViewController *childVC = self.childViewControllers[indexPath.item];
  87. [self.parentViewController addChildViewController:childVC];
  88. [cell.contentView addSubview:childVC.view];
  89. childVC.view.frame = cell.contentView.frame;
  90. [childVC didMoveToParentViewController:self.parentViewController];
  91. return cell;
  92. }
  93. #pragma mark - - - UIScrollViewDelegate
  94. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  95. _startOffsetX = scrollView.contentOffset.x;
  96. _isScroll = YES;
  97. if (self.delegatePageContentCollectionView && [self.delegatePageContentCollectionView respondsToSelector:@selector(pageContentCollectionViewWillBeginDragging)]) {
  98. [self.delegatePageContentCollectionView pageContentCollectionViewWillBeginDragging];
  99. }
  100. }
  101. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  102. _isScroll = NO;
  103. CGFloat offsetX = scrollView.contentOffset.x;
  104. // 1、记录上个子控制器下标
  105. _previousCVCIndex = offsetX / scrollView.frame.size.width;
  106. // 2、pageContentCollectionView:index:
  107. if (self.delegatePageContentCollectionView && [self.delegatePageContentCollectionView respondsToSelector:@selector(pageContentCollectionView:index:)]) {
  108. [self.delegatePageContentCollectionView pageContentCollectionView:self index:_previousCVCIndex];
  109. }
  110. // 3、pageContentCollectionViewDidEndDecelerating
  111. if (self.delegatePageContentCollectionView && [self.delegatePageContentCollectionView respondsToSelector:@selector(pageContentCollectionViewDidEndDecelerating)]) {
  112. [self.delegatePageContentCollectionView pageContentCollectionViewDidEndDecelerating];
  113. }
  114. }
  115. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  116. if (_isAnimated == YES && _isScroll == NO) {
  117. return;
  118. }
  119. // 1、定义获取需要的数据
  120. CGFloat progress = 0;
  121. NSInteger originalIndex = 0;
  122. NSInteger targetIndex = 0;
  123. // 2、判断是左滑还是右滑
  124. CGFloat currentOffsetX = scrollView.contentOffset.x;
  125. CGFloat scrollViewW = scrollView.bounds.size.width;
  126. if (currentOffsetX > _startOffsetX) { // 左滑
  127. // 1、计算 progress
  128. progress = currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW);
  129. // 2、计算 originalIndex
  130. originalIndex = currentOffsetX / scrollViewW;
  131. // 3、计算 targetIndex
  132. targetIndex = originalIndex + 1;
  133. if (targetIndex >= self.childViewControllers.count) {
  134. progress = 1;
  135. targetIndex = originalIndex;
  136. }
  137. // 4、如果完全划过去
  138. if (currentOffsetX - _startOffsetX == scrollViewW) {
  139. progress = 1;
  140. targetIndex = originalIndex;
  141. }
  142. } else { // 右滑
  143. // 1、计算 progress
  144. progress = 1 - (currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW));
  145. // 2、计算 targetIndex
  146. targetIndex = currentOffsetX / scrollViewW;
  147. // 3、计算 originalIndex
  148. originalIndex = targetIndex + 1;
  149. if (originalIndex >= self.childViewControllers.count) {
  150. originalIndex = self.childViewControllers.count - 1;
  151. }
  152. }
  153. // 3、pageContentCollectionViewDelegate; 将 progress/sourceIndex/targetIndex 传递给 SGPageTitleView
  154. if (self.delegatePageContentCollectionView && [self.delegatePageContentCollectionView respondsToSelector:@selector(pageContentCollectionView:progress:originalIndex:targetIndex:)]) {
  155. [self.delegatePageContentCollectionView pageContentCollectionView:self progress:progress originalIndex:originalIndex targetIndex:targetIndex];
  156. }
  157. }
  158. #pragma mark - - - 给外界提供的方法,获取 SGPageTitleView 选中按钮的下标
  159. - (void)setPageContentCollectionViewCurrentIndex:(NSInteger)currentIndex {
  160. CGFloat offsetX = currentIndex * self.collectionView.SG_width;
  161. _startOffsetX = offsetX;
  162. // 1、处理内容偏移
  163. if (_previousCVCIndex != currentIndex) {
  164. [self.collectionView setContentOffset:CGPointMake(offsetX, 0) animated:_isAnimated];
  165. }
  166. // 2、记录上个子控制器下标
  167. _previousCVCIndex = currentIndex;
  168. // 3、pageContentCollectionView:index:
  169. if (self.delegatePageContentCollectionView && [self.delegatePageContentCollectionView respondsToSelector:@selector(pageContentCollectionView:index:)]) {
  170. [self.delegatePageContentCollectionView pageContentCollectionView:self index:currentIndex];
  171. }
  172. }
  173. #pragma mark - - - set
  174. - (void)setIsScrollEnabled:(BOOL)isScrollEnabled {
  175. _isScrollEnabled = isScrollEnabled;
  176. if (isScrollEnabled) {
  177. _collectionView.scrollEnabled = YES;
  178. } else {
  179. _collectionView.scrollEnabled = NO;
  180. }
  181. }
  182. - (void)setIsAnimated:(BOOL)isAnimated {
  183. _isAnimated = isAnimated;
  184. }
  185. @end