默认情况pagerHeaderContainerView
被addSubview到当前的列表UIScrollView上面,pagerHeaderContainerView
就是顶部pagerheader(核心业务视图区域)和pinHeader(悬浮分类控制器区域)的容器视图。这样子,列表上下滑动就只是在操作单个列表ScrollView,不会有滚动突然被中断的情况。视图层级如下:
![](https://img.haomeiwen.com/i15533552/24a27625c9cbc584.png)
情况二
当列表在左右切换的时候、列表向上滚动到pinHeder悬浮的时候,pagerHeaderContainerView
被addSubview到JXPagerSmoothView上面,也就是脱离了列表scrollView,达到固定在顶部的效果。视图层级如下:
![](https://img.haomeiwen.com/i15533552/cc8fc94e0aa47bdc.png)
总结:就是在不断切换pagerHeaderContainerView
的父视图,达到淘宝、转转首页的效果。是不是原理很简单?当然使用的代码也很简单!
//
// JXPagerSmoothView.h
// JXPagerViewExample-OC
//
// Created by jiaxin on 2019/11/15.
// Copyright © 2019 jiaxin. All rights reserved.
//
//这个框架不依赖任何框架
//JXPagerSmoothView继承View
//JXPagerSmoothView放一个UICollectionView
//UICollectionView放代理方法的listView listView是一个继承UIScrollView的类
//设置listView的contentInset
//再让listView偏移到(0, - 头部容器的高度)
#import <UIKit/UIKit.h>
@class JXPagerSmoothView;
@protocol JXPagerSmoothViewListViewDelegate <NSObject>
/**
返回listView。如果是vc包裹的就是vc.view;如果是自定义view包裹的,就是自定义view自己。
*/
- (UIView *)listView;
/**
返回JXPagerSmoothViewListViewDelegate内部持有的UIScrollView或UITableView或UICollectionView
*/
- (UIScrollView *)listScrollView;
@optional
- (void)listDidAppear;//UICollectionView的willDisplayCell方法
- (void)listDidDisappear;//UICollectionView的didEndDisplayingCell方法
@end
@protocol JXPagerSmoothViewDataSource <NSObject>
/**
返回页面header的高度
*/
- (CGFloat)heightForPagerHeaderInPagerView:(JXPagerSmoothView *)pagerView;
/**
返回页面header视图
*/
- (UIView *)viewForPagerHeaderInPagerView:(JXPagerSmoothView *)pagerView;
/**
返回悬浮视图的高度
*/
- (CGFloat)heightForPinHeaderInPagerView:(JXPagerSmoothView *)pagerView;
/**
返回悬浮视图
*/
- (UIView *)viewForPinHeaderInPagerView:(JXPagerSmoothView *)pagerView;
/**
返回列表的数量
*/
- (NSInteger)numberOfListsInPagerView:(JXPagerSmoothView *)pagerView;
/**
根据index初始化一个对应列表实例,需要是遵从`JXPagerSmoothViewListViewDelegate`协议的对象。
如果列表是用自定义UIView封装的,就让自定义UIView遵从`JXPagerSmoothViewListViewDelegate`协议,该方法返回自定义UIView即可。
如果列表是用自定义UIViewController封装的,就让自定义UIViewController遵从`JXPagerSmoothViewListViewDelegate`协议,该方法返回自定义UIViewController即可。
@param pagerView pagerView description
@param index index description
@return 新生成的列表实例
*/
- (id<JXPagerSmoothViewListViewDelegate>)pagerView:(JXPagerSmoothView *)pagerView initListAtIndex:(NSInteger)index;
@end
@protocol JXPagerSmoothViewDelegate <NSObject>
- (void)pagerSmoothViewDidScroll:(UIScrollView *)scrollView;//UICollectionView横向滚动的代理方法
@end
@interface JXPagerSmoothView : UIView
/**
当前已经加载过的列表:key就是@(index)值,value是对应的列表。
*/
@property (nonatomic, strong, readonly) NSDictionary <NSNumber *, id<JXPagerSmoothViewListViewDelegate>> *listDict;//子视图字典
@property (nonatomic, strong, readonly) UICollectionView *listCollectionView;//JXPagerSmoothView上放到listCollectionView
@property (nonatomic, assign) NSInteger defaultSelectedIndex;//初始化默认选中的index
@property (nonatomic, weak) id<JXPagerSmoothViewDelegate> delegate;//代理
- (instancetype)initWithDataSource:(id<JXPagerSmoothViewDataSource>)dataSource NS_DESIGNATED_INITIALIZER;//唯一指定的初始化方法
- (instancetype)init NS_UNAVAILABLE;//不能用于初始化 --不可用
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;//不能用于初始化 --不可用
- (instancetype)initWithCoder:(NSCoder *)aDecoder NS_UNAVAILABLE;//不能用于初始化 --不可用
- (void)reloadData;//重置数据
@end
//
// JXPagerSmoothView.m
// JXPagerViewExample-OC
//
// Created by jiaxin on 2019/11/15.
// Copyright © 2019 jiaxin. All rights reserved.
//
#import "JXPagerSmoothView.h"
static NSString *JXPagerSmoothViewCollectionViewCellIdentifier = @"cell";
//UICollectionView的子类有一个属性pagerHeaderContainerView
@interface JXPagerSmoothCollectionView : UICollectionView <UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIView *pagerHeaderContainerView;
@end
@implementation JXPagerSmoothCollectionView
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint point = [touch locationInView:self.pagerHeaderContainerView];
//当点在pagerHeaderContainerView上面JXPagerSmoothCollectionView不能接受点击
if (CGRectContainsPoint(self.pagerHeaderContainerView.bounds, point)) {
return NO;
}
return YES;
}
@end
@interface JXPagerSmoothView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, weak) id<JXPagerSmoothViewDataSource> dataSource;//数据源
@property (nonatomic, strong) JXPagerSmoothCollectionView *listCollectionView;//JXPagerSmoothView上放到listCollectionView
@property (nonatomic, strong) NSMutableDictionary <NSNumber *, id<JXPagerSmoothViewListViewDelegate>> *listDict;//子视图字典
@property (nonatomic, strong) NSMutableDictionary <NSNumber *, UIView*> *listHeaderDict;//子视图上面的头部视图字典
@property (nonatomic, assign, getter=isSyncListContentOffsetEnabled) BOOL syncListContentOffsetEnabled;//当悬浮标题在导航栏下面的时候,所有子视图都需要恢复到初始的位置 (是否可以设置所有子视图的偏移量)
@property (nonatomic, strong) UIView *pagerHeaderContainerView;//头部视图容器
@property (nonatomic, assign) CGFloat currentPagerHeaderContainerViewY;//当前头部容器视图的Y值
@property (nonatomic, assign) NSInteger currentIndex;//当前索引
@property (nonatomic, strong) UIScrollView *currentListScrollView;//当前子视图
@property (nonatomic, assign) CGFloat heightForPagerHeader;//头部主内容高度
@property (nonatomic, assign) CGFloat heightForPinHeader;//悬浮标题高度
@property (nonatomic, assign) CGFloat heightForPagerHeaderContainerView;//头部视图容器高度
@property (nonatomic, assign) CGFloat currentListInitializeContentOffsetY;//设置当前容器视图的Y值
@property (nonatomic, strong) UIScrollView *singleScrollView;//当数据源为零是创建承载头部容器视图
@end
@implementation JXPagerSmoothView
//生命周期结束移除监听
- (void)dealloc
{
for (id<JXPagerSmoothViewListViewDelegate> list in self.listDict.allValues) {
[[list listScrollView] removeObserver:self forKeyPath:@"contentOffset"];
[[list listScrollView] removeObserver:self forKeyPath:@"contentSize"];
}
}
//唯一指定初始化器
- (instancetype)initWithDataSource:(id<JXPagerSmoothViewDataSource>)dataSource
{
self = [super initWithFrame:CGRectZero];//初始化时为CGRectZero
if (self) {
_dataSource = dataSource;//设置数据源
_listDict = [NSMutableDictionary dictionary];//初始化子视图字典
_listHeaderDict = [NSMutableDictionary dictionary];//初始化子视图头部字典
[self initializeViews];//设置值
}
return self;
}
//设置值
- (void)initializeViews {
self.pagerHeaderContainerView = [[UIView alloc] init];//创建一个pagerHeaderContainerView承载视图
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 0;
_listCollectionView = [[JXPagerSmoothCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
self.listCollectionView.dataSource = self;
self.listCollectionView.delegate = self;
self.listCollectionView.pagingEnabled = YES;
self.listCollectionView.bounces = NO;
self.listCollectionView.showsHorizontalScrollIndicator = NO;
self.listCollectionView.scrollsToTop = NO;
[self.listCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:JXPagerSmoothViewCollectionViewCellIdentifier];
//UICollectionView 开启是否开启预加载,如果开启,cell在没显示的时候就回去调用cellForIndex…方法,如果没开启,cell只有在显示的时候才会去调用cellForIndex…方法
if (@available(iOS 10.0, *)) {
self.listCollectionView.prefetchingEnabled = NO;//相当于懒加载
}
if (@available(iOS 11.0, *)) {
self.listCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//让滑动视图不偏移
}
_listCollectionView.pagerHeaderContainerView = self.pagerHeaderContainerView;//设置一个pagerHeaderContainerView视图
[self addSubview:self.listCollectionView];
}
//这个方法调用比cell早
- (void)layoutSubviews {
[super layoutSubviews];
self.listCollectionView.frame = self.bounds;
if (CGRectEqualToRect(self.pagerHeaderContainerView.frame, CGRectZero)) {//头部容器视图为零时
[self reloadData];
}
if (self.singleScrollView != nil) {//singleScrollView不等于空时
self.singleScrollView.frame = self.bounds;//设置singleScrollView的frame
}
}
- (void)reloadData {
self.currentListScrollView = nil;
self.currentIndex = self.defaultSelectedIndex;
self.currentPagerHeaderContainerViewY = 0;
self.syncListContentOffsetEnabled = NO;
[self.listHeaderDict removeAllObjects];//listHeaderDict的头部视图全部移除
for (id<JXPagerSmoothViewListViewDelegate> list in self.listDict.allValues) {//移除所有子视图的监听
[[list listScrollView] removeObserver:self forKeyPath:@"contentOffset"];
[[list listScrollView] removeObserver:self forKeyPath:@"contentSize"];
[[list listView] removeFromSuperview];
}
[_listDict removeAllObjects];//listHeaderDict有值时全部移除
self.heightForPagerHeader = [self.dataSource heightForPagerHeaderInPagerView:self];//头部内容高度
self.heightForPinHeader = [self.dataSource heightForPinHeaderInPagerView:self];//悬浮标题高度
self.heightForPagerHeaderContainerView = self.heightForPagerHeader + self.heightForPinHeader;//头部容器高度
UIView *pagerHeader = [self.dataSource viewForPagerHeaderInPagerView:self];//头部内容视图
UIView *pinHeader = [self.dataSource viewForPinHeaderInPagerView:self];//悬浮标题视图
[self.pagerHeaderContainerView addSubview:pagerHeader];//添加到头部容器视图上面
[self.pagerHeaderContainerView addSubview:pinHeader];//添加到头部容器视图上面
//设置头部容器视图的frame
self.pagerHeaderContainerView.frame = CGRectMake(0, 0, self.bounds.size.width, self.heightForPagerHeaderContainerView);
//头部内容视图frame
pagerHeader.frame = CGRectMake(0, 0, self.bounds.size.width, self.heightForPagerHeader);
//悬浮标题视图frame
pinHeader.frame = CGRectMake(0, self.heightForPagerHeader, self.bounds.size.width, self.heightForPinHeader);
//让listCollectionView偏移到默认的位置
[self.listCollectionView setContentOffset:CGPointMake(self.listCollectionView.bounds.size.width*self.defaultSelectedIndex, 0) animated:NO];
[self.listCollectionView reloadData];
//如果数据源为零,创建singleScrollView视图
if ([self.dataSource numberOfListsInPagerView:self] == 0) {
self.singleScrollView = [[UIScrollView alloc] init];
[self addSubview:self.singleScrollView];
[self.singleScrollView addSubview:pagerHeader];
self.singleScrollView.contentSize = CGSizeMake(self.bounds.size.width, self.heightForPagerHeader);
self.singleScrollView.backgroundColor = [UIColor orangeColor];
}else if (self.singleScrollView != nil) {//数据源不为零的时候移除singleScrollView视图并且置空
[self.singleScrollView removeFromSuperview];
self.singleScrollView = nil;
}
}
#pragma mark - UICollectionViewDataSource & UICollectionViewDelegateFlowLayout
//listCollectionView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return self.bounds.size;
}
//listCollectionView数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.dataSource numberOfListsInPagerView:self];
}
//listCollectionView的cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:JXPagerSmoothViewCollectionViewCellIdentifier forIndexPath:indexPath];
//根据索引获取子视图
id<JXPagerSmoothViewListViewDelegate> list = self.listDict[@(indexPath.item)];
//如果子视图为空
if (list == nil) {
//根据索引获取子视图
list = [self.dataSource pagerView:self initListAtIndex:indexPath.item];
_listDict[@(indexPath.item)] = list;
[[list listView] setNeedsLayout];
[[list listView] layoutIfNeeded];
//如果子视图是UITableView需要的设置
UIScrollView *listScrollView = [list listScrollView];
if ([listScrollView isKindOfClass:[UITableView class]]) {
((UITableView *)listScrollView).estimatedRowHeight = 0;
((UITableView *)listScrollView).estimatedSectionFooterHeight = 0;
((UITableView *)listScrollView).estimatedSectionHeaderHeight = 0;
}
if (@available(iOS 11.0, *)) {//让滑动视图不偏移
listScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
//设置子视图的contentInset
listScrollView.contentInset = UIEdgeInsetsMake(self.heightForPagerHeaderContainerView, 0, 0, 0);
//
self.currentListInitializeContentOffsetY = -listScrollView.contentInset.top + MIN(-self.currentPagerHeaderContainerViewY, self.heightForPagerHeader);
//设置子视图Y值的偏移量
listScrollView.contentOffset = CGPointMake(0, self.currentListInitializeContentOffsetY);
UIView *listHeader = [[UIView alloc] initWithFrame:CGRectMake(0, -self.heightForPagerHeaderContainerView, self.bounds.size.width, self.heightForPagerHeaderContainerView)];
//子视图添加头部视图
[listScrollView addSubview:listHeader];
//如果头部容器视图的superview为空
if (self.pagerHeaderContainerView.superview == nil) {
//子视图的头部添加头部容器视图
[listHeader addSubview:self.pagerHeaderContainerView];
}
//存储子视图的头部视图
self.listHeaderDict[@(indexPath.item)] = listHeader;
//设置子视图的监听
[listScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
[listScrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}
//设置对应索引子视图是否能够状态栏置顶操作
for (id<JXPagerSmoothViewListViewDelegate> listItem in self.listDict.allValues) {
[listItem listScrollView].scrollsToTop = (listItem == list);
}
//获取子视图view
UIView *listView = [list listView];
//如果子视图不为空 并且 子视图的父视图不为cell.contentView
if (listView != nil && listView.superview != cell.contentView) {
//移除cell.contentView所有的子视图
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
//子视图的framecell.contentView大小
listView.frame = cell.contentView.bounds;
[cell.contentView addSubview:listView];
}
return cell;
}
//子视图即将出现调用
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
[self listDidAppear:indexPath.item];
}
////子视图即将消失调用
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
[self listDidDisappear:indexPath.item];
}
//UIScrollView正在滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//UICollectionView横向滚动的代理方法
if (self.delegate && [self.delegate respondsToSelector:@selector(pagerSmoothViewDidScroll:)]) {
[self.delegate pagerSmoothViewDidScroll:scrollView];
}
//获取索引
NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
//根据索引获取相应的子视图
UIScrollView *listScrollView = [self.listDict[@(index)] listScrollView];
//如果索引不等于当前索引 并且 scrollView没有拖拽或减速 并且 子视图的Y值偏移量小于或等于负的悬浮标题的高度 (就是悬浮标题在导航栏的下面没有接触到导航栏)
if (index != self.currentIndex && !(scrollView.isDragging || scrollView.isDecelerating) && listScrollView.contentOffset.y <= -self.heightForPinHeader) {
//列表左右切换滚动结束之后,需要把pagerHeaderContainerView添加到当前index的列表上面
[self horizontalScrollDidEndAtIndex:index];
}else {
//左右滚动的时候,就把listHeaderContainerView添加到self,达到悬浮在顶部的效果
if (self.pagerHeaderContainerView.superview != self) {
//把pagerHeaderContainerView添加到JXPageSmoothView上面
self.pagerHeaderContainerView.frame = CGRectMake(0, self.currentPagerHeaderContainerViewY, self.pagerHeaderContainerView.bounds.size.width, self.pagerHeaderContainerView.bounds.size.height);
[self addSubview:self.pagerHeaderContainerView];
}
}
//设置当前索引
if (index != self.currentIndex) {
self.currentIndex = index;
}
}
//用户已经停止拖拽scrollView,就会调用这个方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {//scrollView停止滚动,完全静止
NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
//列表左右切换滚动结束之后,需要把pagerHeaderContainerView添加到当前index的列表上面
[self horizontalScrollDidEndAtIndex:index];
}
//方便测试自己加的
if (decelerate == NO) {
NSLog(@"scrollView停止滚动,完全静止");
} else {
NSLog(@"用户停止拖拽,但是scrollView由于惯性,会继续滚动,并且减速");
}
}
//scrollView已经停止减速,就会调用这个方法(停止滚动)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
//列表左右切换滚动结束之后,需要把pagerHeaderContainerView添加到当前index的列表上面
[self horizontalScrollDidEndAtIndex:index];
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"contentOffset"]) {//监听子视图contentOffset的变化
UIScrollView *scrollView = (UIScrollView *)object;
if (scrollView != nil) {//子视图不为空
//contentOffset发生变化时调用listDidScroll
[self listDidScroll:scrollView];
}
}else if([keyPath isEqualToString:@"contentSize"]) {//监听子视图contentSize的变化
UIScrollView *scrollView = (UIScrollView *)object;
if (scrollView != nil) {//子视图不为空
//求minContentSizeHeight
CGFloat minContentSizeHeight = self.bounds.size.height - self.heightForPinHeader;
//如果minContentSizeHeight大于子视图的contentSize的高度
if (minContentSizeHeight > scrollView.contentSize.height) {
//设置子视图的contentSize为最大的
scrollView.contentSize = CGSizeMake(scrollView.contentSize.width, minContentSizeHeight);
//新的scrollView第一次加载的时候重置contentOffset
//当前子视图不等于空 并且 子视图不等于当前子视图
if (_currentListScrollView != nil && scrollView != _currentListScrollView) {
//设置子视图的偏移点为初始化偏移点
scrollView.contentOffset = CGPointMake(0, self.currentListInitializeContentOffsetY);
}
}
}
}else {
//不用管
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark - Event
//监听子视图正在滚动的Event
- (void)listDidScroll:(UIScrollView *)scrollView {
//如果子视图正在滚动或正在减速 直接return;
if (self.listCollectionView.isDragging || self.listCollectionView.isDecelerating) {
return;
}
//根据子视图获取索引
NSInteger listIndex = [self listIndexForListScrollView:scrollView];
//如果获取的索引不等于当前记录的索引 直接return;
if (listIndex != self.currentIndex) {
return;
}
//设置当前的子视图
self.currentListScrollView = scrollView;
//计算子视图Y值的偏移量
CGFloat contentOffsetY = scrollView.contentOffset.y + self.heightForPagerHeaderContainerView;
//偏移量 小于 头部主内容高度
if (contentOffsetY < self.heightForPagerHeader) {
//是否可以设置所有子视图的偏移量 设置为YES
self.syncListContentOffsetEnabled = YES;
//设置当前容器视图的Y值为-contentOffsetY;
self.currentPagerHeaderContainerViewY = -contentOffsetY;
for (id<JXPagerSmoothViewListViewDelegate> list in self.listDict.allValues) {
if ([list listScrollView] != self.currentListScrollView) {
//设置不是当前子视图的Y值偏移量为(scrollView.contentOffset)
[[list listScrollView] setContentOffset:scrollView.contentOffset animated:NO];
}
}
//获取子视图的头部视图
UIView *listHeader = [self listHeaderForListScrollView:scrollView];
//如果头部容器视图的superview 不等于 子视图的头部视图
if (self.pagerHeaderContainerView.superview != listHeader) {
//把头部容器视图 添加到 子视图的头部视图
self.pagerHeaderContainerView.frame = CGRectMake(0, 0, self.pagerHeaderContainerView.bounds.size.width, self.pagerHeaderContainerView.bounds.size.height);
[listHeader addSubview:self.pagerHeaderContainerView];
}
}else {
//如果pagerHeaderContainerView的superview为空
if (self.pagerHeaderContainerView.superview != self) {
//把pagerHeaderContainerView添加到JXPageSmoothView上
self.pagerHeaderContainerView.frame = CGRectMake(0, -self.heightForPagerHeader, self.pagerHeaderContainerView.bounds.size.width, self.pagerHeaderContainerView.bounds.size.height);
[self addSubview:self.pagerHeaderContainerView];
}
//是否可以设置所有子视图的偏移量 为YES
if (self.isSyncListContentOffsetEnabled) {
//是否可以设置所有子视图的偏移量 设置为NO
self.syncListContentOffsetEnabled = NO;
//设置当前容器视图的Y值为-self.heightForPagerHeader; 即刚好悬浮标题在导航栏下
self.currentPagerHeaderContainerViewY = -self.heightForPagerHeader;
for (id<JXPagerSmoothViewListViewDelegate> list in self.listDict.allValues) {
if ([list listScrollView] != scrollView) {
//设置不是当前子视图的Y值偏移量为(0, -self.heightForPinHeader) 即悬浮标题下面(悬浮标题下导航栏下的那种)
[[list listScrollView] setContentOffset:CGPointMake(0, -self.heightForPinHeader) animated:NO];
}
}
}
}
}
#pragma mark - Private
//获取子视图上面的头部视图
- (UIView *)listHeaderForListScrollView:(UIScrollView *)scrollView {
for (NSNumber *index in self.listDict) {
if ([self.listDict[index] listScrollView] == scrollView) {
return self.listHeaderDict[index];
}
}
return nil;
}
//获取子视图
- (NSInteger)listIndexForListScrollView:(UIScrollView *)scrollView {
for (NSNumber *index in self.listDict) {
if ([self.listDict[index] listScrollView] == scrollView) {
return [index integerValue];
}
}
return 0;
}
//子视图已经出现是判空等操作,调用代理
- (void)listDidAppear:(NSInteger)index {
NSUInteger count = [self.dataSource numberOfListsInPagerView:self];
if (count <= 0 || index >= count) {
return;
}
id<JXPagerSmoothViewListViewDelegate> list = self.listDict[@(index)];
if (list && [list respondsToSelector:@selector(listDidAppear)]) {
[list listDidAppear];
}
}
//子视图已经消失是判空等操作,调用代理
- (void)listDidDisappear:(NSInteger)index {
NSUInteger count = [self.dataSource numberOfListsInPagerView:self];
if (count <= 0 || index >= count) {
return;
}
id<JXPagerSmoothViewListViewDelegate> list = self.listDict[@(index)];
if (list && [list respondsToSelector:@selector(listDidDisappear)]) {
[list listDidDisappear];
}
}
/// 列表左右切换滚动结束之后,需要把pagerHeaderContainerView添加到当前index的列表上面
- (void)horizontalScrollDidEndAtIndex:(NSInteger)index {
self.currentIndex = index;
UIView *listHeader = self.listHeaderDict[@(index)];//获取对应索引的子视图的头部视图
UIScrollView *listScrollView = [self.listDict[@(index)] listScrollView];//获取对应索引的子视图
//当子视图的头部视图 和 子视图的Y值偏移量小于或等于负的悬浮标题的高度 (就是悬浮标题在导航栏的下面没有接触到导航栏)
if (listHeader != nil && listScrollView.contentOffset.y <= -self.heightForPinHeader) {
for (id<JXPagerSmoothViewListViewDelegate> listItem in self.listDict.allValues) {
//设置对应索引子视图是否能够状态栏置顶操作
[listItem listScrollView].scrollsToTop = ([listItem listScrollView] == listScrollView);
}
//设置头部容器视图的frame
self.pagerHeaderContainerView.frame = CGRectMake(0, 0, self.pagerHeaderContainerView.bounds.size.width, self.pagerHeaderContainerView.bounds.size.height);
//对应索引的子视图的头部视图添加头部容器视图
[listHeader addSubview:self.pagerHeaderContainerView];
}
}
@end
网友评论