HHLinkageViewController
collectionView嵌套tableView实现悬停交互
Install / Use
/learn @yuwind/HHLinkageViewControllerREADME
##collectionView嵌套tableView实现悬停交互
###效果图

###使用方法
1、子类需要继承
HHLinkageViewController2、设置常用属性,中间titles高度为宏定义LINKAGEVIEWH
@property (nonatomic, copy) NSArray <UIViewController *>*viewControllers;//子控制器
@property (nonatomic, strong, readonly) HHLinkageView *linkageView;//只读中间标题视图
@property (nonatomic, strong) UIView *headerView;//自定义头部试图,可以只设置高度
@property (nonatomic, copy) NSArray *titlesArray;//中间标题
@property (nonatomic, assign) BOOL isNeedLessMove;//数据少于屏幕是否需要滑动,默认YES
3子控制器
scrollViewDidScroll中分别调用父控制器如下方法
/**
@param scrollView 子控制器tableView
@param index 子控制器所在的索引值
*/
- (void)childScrollViewDidScroll:(UIScrollView *)scrollView index:(NSInteger)index;
###基本讲解
我们知道
frame是相对于父控件的位置,bounds是相对于自己的位置,可以用来改变子视图的位置,我的出发点就是通过改变tableView的bounds实现的,核心代码也就50行左右。
我们的目的是tableView上下滑动的时候,改变collectionView的y值,同时tableView的contentOffset不变。
1、 我们需要先判断滑动方向,调用如下方法
CGPoint velocity = [scrollView.panGestureRecognizer velocityInView:nil];
同时引入中间变量_direction,因为当手指离开屏幕时velocity会变为0,我们需要保留离开屏幕时的方向。
2、 我们先考虑向上滑动的情况,箭头为滑动方向
情景一不包含偏移量,如下图

我们只需要把collectionView的y减去滑动的offset,同时把scrollView的bounds的y设置为0,这样collectionView就可以滑动了,而scrollView却不变
_collectionView.y -= offsetY;
CGRect tempFrame = scrollView.bounds;
tempFrame.origin.y = 0;
scrollView.bounds = tempFrame;
情景二包含偏移量,如下图

我们需要引入一个变量offsetArray,用来保存上一次滑动之后的偏移量,在赋值的时候减去详细的偏移量。需要注意的是改变scrollView.bounds的y的时候,不能直接赋值为上次的保留值conserverOffset,需要向上偏移一点,这样在快速滑动松手的时候,才会有惯性。
CGFloat conserverOffset = _offsetArray[index].floatValue;
if (conserverOffset) {
_collectionView.y -= (offsetY-conserverOffset);
CGRect tempFrame = scrollView.bounds;
tempFrame.origin.y = conserverOffset+0.3;//0.3是为了保留惯性
scrollView.bounds = tempFrame;
}
3、 方向向下和向上的差不多,具体看下代码吧
最后统一处理边界值就可以了,是不是特别简单。
if (_collectionView.y>=_headerView.height+LINKAGEVIEWH) {
_collectionView.y = _headerView.height+LINKAGEVIEWH;
_linkageView.y = _headerView.height;
}else if (_collectionView.y<=LINKAGEVIEWH){
_collectionView.y = LINKAGEVIEWH;
_linkageView.y = 0;
}else{
_linkageView.y = _collectionView.y-LINKAGEVIEWH;
}
4、 cocoapod下载
target 'MyApp' do
pod 'HHLinkageViewController', '~> 1.0.0'
end
#####简书地址
Related Skills
node-connect
349.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.5kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
