Aucune description

SGPageContentScrollView.m 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // SGPageContentScrollView.m
  3. // SGPagingViewExample
  4. //
  5. // Created by kingsic on 2017/7/21.
  6. // Copyright © 2017年 kingsic. All rights reserved.
  7. //
  8. #import "SGPageContentScrollView.h"
  9. #import "UIView+SGPagingView.h"
  10. @interface SGPageContentScrollView () <UIScrollViewDelegate>
  11. // 外界父控制器
  12. @property (nonatomic, weak) UIViewController *parentViewController;
  13. /// 存储子控制器
  14. @property (nonatomic, strong) NSArray *childViewControllers;
  15. /// scrollView
  16. @property (nonatomic, strong) UIScrollView *scrollView;
  17. /// 记录刚开始时的偏移量
  18. @property (nonatomic, assign) NSInteger startOffsetX;
  19. /// 记录加载的上个子控制器
  20. @property (nonatomic, weak) UIViewController *previousCVC;
  21. /// 记录加载的上个子控制器的下标
  22. @property (nonatomic, assign) NSInteger previousCVCIndex;
  23. /// 标记内容滚动
  24. @property (nonatomic, assign) BOOL isScroll;
  25. @end
  26. @implementation SGPageContentScrollView
  27. - (instancetype)initWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs {
  28. if (self = [super initWithFrame:frame]) {
  29. if (parentVC == nil) {
  30. @throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageContentScrollView 初始化方法中所在控制器必须设置" userInfo:nil];
  31. }
  32. self.parentViewController = parentVC;
  33. if (childVCs == nil) {
  34. @throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageContentScrollView 初始化方法中子控制器必须设置" userInfo:nil];
  35. }
  36. self.childViewControllers = childVCs;
  37. [self initialization];
  38. [self setupSubviews];
  39. }
  40. return self;
  41. }
  42. + (instancetype)pageContentScrollViewWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs {
  43. return [[self alloc] initWithFrame:frame parentVC:parentVC childVCs:childVCs];
  44. }
  45. - (void)initialization {
  46. _startOffsetX = 0;
  47. _previousCVCIndex = -1;
  48. }
  49. - (void)setupSubviews {
  50. // 0、处理偏移量
  51. UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero];
  52. [self addSubview:tempView];
  53. // 1、添加 scrollView
  54. [self addSubview:self.scrollView];
  55. }
  56. - (UIScrollView *)scrollView {
  57. if (!_scrollView) {
  58. _scrollView = [[UIScrollView alloc] init];
  59. _scrollView.frame = self.bounds;
  60. _scrollView.bounces = NO;
  61. _scrollView.delegate = self;
  62. _scrollView.pagingEnabled = YES;
  63. _scrollView.showsVerticalScrollIndicator = NO;
  64. _scrollView.showsHorizontalScrollIndicator = NO;
  65. CGFloat contentWidth = self.childViewControllers.count * _scrollView.SG_width;
  66. _scrollView.contentSize = CGSizeMake(contentWidth, 0);
  67. }
  68. return _scrollView;
  69. }
  70. #pragma mark - - - UIScrollViewDelegate
  71. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  72. _startOffsetX = scrollView.contentOffset.x;
  73. _isScroll = YES;
  74. if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollViewWillBeginDragging)]) {
  75. [self.delegatePageContentScrollView pageContentScrollViewWillBeginDragging];
  76. }
  77. }
  78. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  79. _isScroll = NO;
  80. // 1、根据标题下标计算 pageContent 偏移量
  81. CGFloat offsetX = scrollView.contentOffset.x;
  82. // 2、切换子控制器的时候,执行上个子控制器的 viewWillDisappear 方法
  83. if (_startOffsetX != offsetX) {
  84. [self.previousCVC beginAppearanceTransition:NO animated:NO];
  85. }
  86. // 3、获取当前显示子控制器的下标
  87. NSInteger index = offsetX / scrollView.frame.size.width;
  88. // 4、添加子控制器及子控制器的 view 到父控制器以及父控制器 view 中
  89. UIViewController *childVC = self.childViewControllers[index];
  90. BOOL firstAdd = NO;
  91. if (![self.parentViewController.childViewControllers containsObject:childVC]) {
  92. [self.parentViewController addChildViewController:childVC];
  93. firstAdd = YES;
  94. }
  95. [childVC beginAppearanceTransition:YES animated:NO];
  96. if (firstAdd) {
  97. [self.scrollView addSubview:childVC.view];
  98. childVC.view.frame = CGRectMake(offsetX, 0, self.SG_width, self.SG_height);
  99. }
  100. // 2.1、切换子控制器的时候,执行上个子控制器的 viewDidDisappear 方法
  101. if (_startOffsetX != offsetX) {
  102. [self.previousCVC endAppearanceTransition];
  103. }
  104. [childVC endAppearanceTransition];
  105. if (firstAdd) {
  106. [childVC didMoveToParentViewController:self.parentViewController];
  107. }
  108. // 4.1、记录上个展示的子控制器、记录当前子控制器偏移量
  109. self.previousCVC = childVC;
  110. _previousCVCIndex = index;
  111. // 5、pageContentScrollView:index:
  112. if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollView:index:)]) {
  113. [self.delegatePageContentScrollView pageContentScrollView:self index:index];
  114. }
  115. // 6、pageContentScrollViewDidEndDecelerating
  116. if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollViewDidEndDecelerating)]) {
  117. [self.delegatePageContentScrollView pageContentScrollViewDidEndDecelerating];
  118. }
  119. }
  120. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  121. if (_isAnimated == YES && _isScroll == NO) {
  122. return;
  123. }
  124. // 1、定义获取需要的数据
  125. CGFloat progress = 0;
  126. NSInteger originalIndex = 0;
  127. NSInteger targetIndex = 0;
  128. // 2、判断是左滑还是右滑
  129. CGFloat currentOffsetX = scrollView.contentOffset.x;
  130. CGFloat scrollViewW = scrollView.bounds.size.width;
  131. if (currentOffsetX > _startOffsetX) { // 左滑
  132. // 1、计算 progress
  133. progress = currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW);
  134. // 2、计算 originalIndex
  135. originalIndex = currentOffsetX / scrollViewW;
  136. // 3、计算 targetIndex
  137. targetIndex = originalIndex + 1;
  138. if (targetIndex >= self.childViewControllers.count) {
  139. progress = 1;
  140. targetIndex = self.childViewControllers.count - 1;
  141. }
  142. // 4、如果完全划过去
  143. if (currentOffsetX - _startOffsetX == scrollViewW) {
  144. progress = 1;
  145. targetIndex = originalIndex;
  146. }
  147. } else { // 右滑
  148. // 1、计算 progress
  149. progress = 1 - (currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW));
  150. // 2、计算 targetIndex
  151. targetIndex = currentOffsetX / scrollViewW;
  152. // 3、计算 originalIndex
  153. originalIndex = targetIndex + 1;
  154. if (originalIndex >= self.childViewControllers.count) {
  155. originalIndex = self.childViewControllers.count - 1;
  156. }
  157. }
  158. // 3、pageContentScrollViewDelegate; 将 progress/sourceIndex/targetIndex 传递给 SGPageTitleView
  159. if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollView:progress:originalIndex:targetIndex:)]) {
  160. [self.delegatePageContentScrollView pageContentScrollView:self progress:progress originalIndex:originalIndex targetIndex:targetIndex];
  161. }
  162. }
  163. #pragma mark - - - 给外界提供的方法,获取 SGPageTitleView 选中按钮的下标
  164. - (void)setPageContentScrollViewCurrentIndex:(NSInteger)currentIndex {
  165. // 1、根据标题下标计算 pageContent 偏移量
  166. CGFloat offsetX = currentIndex * self.SG_width;
  167. // 2、切换子控制器的时候,执行上个子控制器的 viewWillDisappear 方法
  168. if (self.previousCVC != nil && _previousCVCIndex != currentIndex) {
  169. [self.previousCVC beginAppearanceTransition:NO animated:NO];
  170. }
  171. // 3、添加子控制器及子控制器的 view 到父控制器以及父控制器 view 中
  172. if (_previousCVCIndex != currentIndex) {
  173. UIViewController *childVC = self.childViewControllers[currentIndex];
  174. BOOL firstAdd = NO;
  175. if (![self.parentViewController.childViewControllers containsObject:childVC]) {
  176. [self.parentViewController addChildViewController:childVC];
  177. firstAdd = YES;
  178. }
  179. [childVC beginAppearanceTransition:YES animated:NO];
  180. if (firstAdd) {
  181. [self.scrollView addSubview:childVC.view];
  182. childVC.view.frame = CGRectMake(offsetX, 0, self.SG_width, self.SG_height);
  183. }
  184. // 1.1、切换子控制器的时候,执行上个子控制器的 viewDidDisappear 方法
  185. if (self.previousCVC != nil && _previousCVCIndex != currentIndex) {
  186. [self.previousCVC endAppearanceTransition];
  187. }
  188. [childVC endAppearanceTransition];
  189. if (firstAdd) {
  190. [childVC didMoveToParentViewController:self.parentViewController];
  191. }
  192. // 3.1、记录上个子控制器
  193. self.previousCVC = childVC;
  194. // 4、处理内容偏移
  195. [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:_isAnimated];
  196. }
  197. // 3.2、记录上个子控制器下标
  198. _previousCVCIndex = currentIndex;
  199. // 3.3、重置 _startOffsetX
  200. _startOffsetX = offsetX;
  201. // 5、pageContentScrollView:index:
  202. if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollView:index:)]) {
  203. [self.delegatePageContentScrollView pageContentScrollView:self index:currentIndex];
  204. }
  205. }
  206. #pragma mark - - - set
  207. - (void)setIsScrollEnabled:(BOOL)isScrollEnabled {
  208. _isScrollEnabled = isScrollEnabled;
  209. if (isScrollEnabled) {
  210. _scrollView.scrollEnabled = YES;
  211. } else {
  212. _scrollView.scrollEnabled = NO;
  213. }
  214. }
  215. - (void)setIsAnimated:(BOOL)isAnimated {
  216. _isAnimated = isAnimated;
  217. }
  218. @end