猎豆优选

SGPageContentScrollView.m 7.1KB

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