123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- //
- // SGPageContentScrollView.m
- // SGPagingViewExample
- //
- // Created by kingsic on 2017/7/21.
- // Copyright © 2017年 kingsic. All rights reserved.
- //
- #import "SGPageContentScrollView.h"
- #import "UIView+SGPagingView.h"
- @interface SGPageContentScrollView () <UIScrollViewDelegate>
- // 外界父控制器
- @property (nonatomic, weak) UIViewController *parentViewController;
- /// 存储子控制器
- @property (nonatomic, strong) NSArray *childViewControllers;
- /// scrollView
- @property (nonatomic, strong) UIScrollView *scrollView;
- /// 记录刚开始时的偏移量
- @property (nonatomic, assign) NSInteger startOffsetX;
- /// 记录加载的上个子控制器
- @property (nonatomic, weak) UIViewController *previousCVC;
- /// 记录加载的上个子控制器的下标
- @property (nonatomic, assign) NSInteger previousCVCIndex;
- /// 标记内容滚动
- @property (nonatomic, assign) BOOL isScroll;
- @end
- @implementation SGPageContentScrollView
- - (instancetype)initWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs {
- if (self = [super initWithFrame:frame]) {
- if (parentVC == nil) {
- @throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageContentScrollView 初始化方法中所在控制器必须设置" userInfo:nil];
- }
- self.parentViewController = parentVC;
- if (childVCs == nil) {
- @throw [NSException exceptionWithName:@"SGPagingView" reason:@"SGPageContentScrollView 初始化方法中子控制器必须设置" userInfo:nil];
- }
- self.childViewControllers = childVCs;
-
- [self initialization];
- [self setupSubviews];
- }
- return self;
- }
- + (instancetype)pageContentScrollViewWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSArray *)childVCs {
- return [[self alloc] initWithFrame:frame parentVC:parentVC childVCs:childVCs];
- }
- - (void)initialization {
- _startOffsetX = 0;
- _previousCVCIndex = -1;
- }
- - (void)setupSubviews {
- // 0、处理偏移量
- UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero];
- [self addSubview:tempView];
- // 1、添加 scrollView
- [self addSubview:self.scrollView];
- }
- - (UIScrollView *)scrollView {
- if (!_scrollView) {
- _scrollView = [[UIScrollView alloc] init];
- _scrollView.frame = self.bounds;
- _scrollView.bounces = NO;
- _scrollView.delegate = self;
- _scrollView.pagingEnabled = YES;
- _scrollView.showsVerticalScrollIndicator = NO;
- _scrollView.showsHorizontalScrollIndicator = NO;
- CGFloat contentWidth = self.childViewControllers.count * _scrollView.SG_width;
- _scrollView.contentSize = CGSizeMake(contentWidth, 0);
- }
- return _scrollView;
- }
- #pragma mark - - - UIScrollViewDelegate
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
- _startOffsetX = scrollView.contentOffset.x;
- _isScroll = YES;
- if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollViewWillBeginDragging)]) {
- [self.delegatePageContentScrollView pageContentScrollViewWillBeginDragging];
- }
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- _isScroll = NO;
- // 1、根据标题下标计算 pageContent 偏移量
- CGFloat offsetX = scrollView.contentOffset.x;
- // 2、切换子控制器的时候,执行上个子控制器的 viewWillDisappear 方法
- if (_startOffsetX != offsetX) {
- [self.previousCVC beginAppearanceTransition:NO animated:NO];
- }
- // 3、获取当前显示子控制器的下标
- NSInteger index = offsetX / scrollView.frame.size.width;
- // 4、添加子控制器及子控制器的 view 到父控制器以及父控制器 view 中
- UIViewController *childVC = self.childViewControllers[index];
-
- BOOL firstAdd = NO;
- if (![self.parentViewController.childViewControllers containsObject:childVC]) {
- [self.parentViewController addChildViewController:childVC];
- firstAdd = YES;
- }
-
- [childVC beginAppearanceTransition:YES animated:NO];
-
- if (firstAdd) {
- [self.scrollView addSubview:childVC.view];
- childVC.view.frame = CGRectMake(offsetX, 0, self.SG_width, self.SG_height);
- }
-
- // 2.1、切换子控制器的时候,执行上个子控制器的 viewDidDisappear 方法
- if (_startOffsetX != offsetX) {
- [self.previousCVC endAppearanceTransition];
- }
- [childVC endAppearanceTransition];
-
- if (firstAdd) {
- [childVC didMoveToParentViewController:self.parentViewController];
- }
- // 4.1、记录上个展示的子控制器、记录当前子控制器偏移量
- self.previousCVC = childVC;
- _previousCVCIndex = index;
-
- // 5、pageContentScrollView:index:
- if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollView:index:)]) {
- [self.delegatePageContentScrollView pageContentScrollView:self index:index];
- }
-
- // 6、pageContentScrollViewDidEndDecelerating
- if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollViewDidEndDecelerating)]) {
- [self.delegatePageContentScrollView pageContentScrollViewDidEndDecelerating];
- }
- }
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
- if (_isAnimated == YES && _isScroll == NO) {
- return;
- }
- // 1、定义获取需要的数据
- CGFloat progress = 0;
- NSInteger originalIndex = 0;
- NSInteger targetIndex = 0;
- // 2、判断是左滑还是右滑
- CGFloat currentOffsetX = scrollView.contentOffset.x;
- CGFloat scrollViewW = scrollView.bounds.size.width;
- if (currentOffsetX > _startOffsetX) { // 左滑
- // 1、计算 progress
- progress = currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW);
- // 2、计算 originalIndex
- originalIndex = currentOffsetX / scrollViewW;
- // 3、计算 targetIndex
- targetIndex = originalIndex + 1;
- if (targetIndex >= self.childViewControllers.count) {
- progress = 1;
- targetIndex = self.childViewControllers.count - 1;
- }
- // 4、如果完全划过去
- if (currentOffsetX - _startOffsetX == scrollViewW) {
- progress = 1;
- targetIndex = originalIndex;
- }
- } else { // 右滑
- // 1、计算 progress
- progress = 1 - (currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW));
- // 2、计算 targetIndex
- targetIndex = currentOffsetX / scrollViewW;
- // 3、计算 originalIndex
- originalIndex = targetIndex + 1;
- if (originalIndex >= self.childViewControllers.count) {
- originalIndex = self.childViewControllers.count - 1;
- }
- }
- // 3、pageContentScrollViewDelegate; 将 progress/sourceIndex/targetIndex 传递给 SGPageTitleView
- if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollView:progress:originalIndex:targetIndex:)]) {
- [self.delegatePageContentScrollView pageContentScrollView:self progress:progress originalIndex:originalIndex targetIndex:targetIndex];
- }
- }
- #pragma mark - - - 给外界提供的方法,获取 SGPageTitleView 选中按钮的下标
- - (void)setPageContentScrollViewCurrentIndex:(NSInteger)currentIndex {
- // 1、根据标题下标计算 pageContent 偏移量
- CGFloat offsetX = currentIndex * self.SG_width;
- // 2、切换子控制器的时候,执行上个子控制器的 viewWillDisappear 方法
- if (self.previousCVC != nil && _previousCVCIndex != currentIndex) {
- [self.previousCVC beginAppearanceTransition:NO animated:NO];
- }
- // 3、添加子控制器及子控制器的 view 到父控制器以及父控制器 view 中
- if (_previousCVCIndex != currentIndex) {
- UIViewController *childVC = self.childViewControllers[currentIndex];
-
- BOOL firstAdd = NO;
- if (![self.parentViewController.childViewControllers containsObject:childVC]) {
- [self.parentViewController addChildViewController:childVC];
- firstAdd = YES;
- }
-
- [childVC beginAppearanceTransition:YES animated:NO];
-
- if (firstAdd) {
- [self.scrollView addSubview:childVC.view];
- childVC.view.frame = CGRectMake(offsetX, 0, self.SG_width, self.SG_height);
- }
-
- // 1.1、切换子控制器的时候,执行上个子控制器的 viewDidDisappear 方法
- if (self.previousCVC != nil && _previousCVCIndex != currentIndex) {
- [self.previousCVC endAppearanceTransition];
- }
- [childVC endAppearanceTransition];
-
- if (firstAdd) {
- [childVC didMoveToParentViewController:self.parentViewController];
- }
-
- // 3.1、记录上个子控制器
- self.previousCVC = childVC;
-
- // 4、处理内容偏移
- [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:_isAnimated];
- }
- // 3.2、记录上个子控制器下标
- _previousCVCIndex = currentIndex;
- // 3.3、重置 _startOffsetX
- _startOffsetX = offsetX;
-
- // 5、pageContentScrollView:index:
- if (self.delegatePageContentScrollView && [self.delegatePageContentScrollView respondsToSelector:@selector(pageContentScrollView:index:)]) {
- [self.delegatePageContentScrollView pageContentScrollView:self index:currentIndex];
- }
- }
- #pragma mark - - - set
- - (void)setIsScrollEnabled:(BOOL)isScrollEnabled {
- _isScrollEnabled = isScrollEnabled;
- if (isScrollEnabled) {
- _scrollView.scrollEnabled = YES;
- } else {
- _scrollView.scrollEnabled = NO;
- }
- }
- - (void)setIsAnimated:(BOOL)isAnimated {
- _isAnimated = isAnimated;
- }
- @end
|