口袋优选

SGPageContentView.m 7.5KB

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