赞
踩
Python实战社群
Java实战社群
长按识别下方二维码,按需求添加
扫码关注添加客服
进Python社群▲
扫码关注添加客服
进Java社群▲
作者 | QuintGao
来源 | 掘金,点击阅读原文查看作者更多文章
之前写的GKPageScrollView https://github.com/QuintGao/GKPageScrollView 已经收获接近900个star,最近有时间对里面的GKPageSmoothView进行了优化升级,顺便实现了豆瓣电影详情页的效果,本文主要对GKPageSmoothView的实现做下简单介绍
先看下豆瓣电影的效果图
GKPageSmoothView与GKPageScrollView的实现原理不同
GKPageSmoothView主要是通过修改listScrollView的contentInset,然后添加headerView,并在滑动到一定程度的时候对headerView进行位置和布局的转换
GKPageScrollView主要是包含mainTableView和listScrollView,并在合适的位置处理滑动冲突
GKPageSmoothView支持以下功能:
支持上下滑动、左右滑动、手势返回等
支持顶部悬停、底部悬停
支持底部悬停拖拽,可实现豆瓣电影详情页效果
支持如JXCategoryView,JXSegmentedView等的分段控件
可实现导航栏颜色渐变、头图下拉放大等效果
支持主页下拉刷新、列表页上拉加载
基本原理可以看这里UIScrollView嵌套滚动完美解决方案:仿淘宝、转转首页 下面主要讲讲底部悬停及底部拖拽的实现
底部悬停的实现跟顶部悬停的实现基本一样,前提是headerView的高度要高于屏幕的高度,这样当headerView下滑到可以悬浮的位置时将segmentedView添加到GKPageSmoothView上固定,不再跟随listScrollView滑动,下面看下主要代码
- // 滑动到临界位置,固定segmentedView
- if (contentOffsetY < (self.headerContainerHeight - self.frame.size.height)) {
- self.hoverType = GKPageSmoothHoverTypeBottom;
- if (self.segmentedView.superview != self.bottomContainerView) {
- self.bottomContainerView.hidden = NO;
- CGRect frame = self.segmentedView.frame;
- frame.origin.y = 0;
- self.segmentedView.frame = frame;
- [self.bottomContainerView addSubview:self.segmentedView];
- }
- }else {
- // 超过临界位置,segmentedView跟listScrollView一起滑动
- if (self.segmentedView.superview != self.headerContainerView) {
- self.bottomContainerView.hidden = YES;
- CGRect frame = self.segmentedView.frame;
- frame.origin.y = self.headerHeight;
- self.segmentedView.frame = frame;
- [self.headerContainerView addSubview:self.segmentedView];
- }
- }
通过观察豆瓣详情页发现,拖拽底部时segmentedView和listScrollView是一起滑动的,所以添加了bottomContainerView来处理,并添加拖拽手势,当拖拽bottomContainerView时,让headerContainerView保持固定不动,segmentedView和listScrollView根据bottomContainerView一起滑动,此时只需处理拖拽手势和listScrollView的滑动即可
1、开始拖拽时,处理headerContainerView和listCollectionView
- // 将headerContainerView添加到self
- if (self.headerContainerView.superview != self) {
- CGRect frame = self.headerContainerView.frame;
- frame.origin.y = -(self.currentListScrollView.contentOffset.y + self.headerContainerHeight);
- self.headerContainerView.frame = frame;
- [self insertSubview:self.headerContainerView belowSubview:self.bottomContainerView];
- }
-
- // 将listCollectionView添加到bottomContainerView
- if (self.listCollectionView.superview != self.bottomContainerView) {
- CGRect frame = self.listCollectionView.frame;
- frame.origin.y = self.segmentedHeight;
- frame.size.height = self.bottomContainerView.frame.size.height - self.segmentedHeight;
- self.listCollectionView.frame = frame;
- [self.bottomContainerView addSubview:self.listCollectionView];
- self->_listCollectionView.headerContainerView = nil;
-
- // 记录当前列表的滑动位置
- self.currentListPanBeganContentOffsetY = self.currentListScrollView.contentOffset.y;
-
- for (id<GKPageSmoothListViewDelegate> list in self.listDict.allValues) {
- list.listScrollView.contentInset = UIEdgeInsetsZero;
- list.listScrollView.contentOffset = CGPointZero;
-
- CGRect frame = list.listView.frame;
- frame.size = self.listCollectionView.bounds.size;
- list.listView.frame = frame;
- }
- }
2、结束拖拽时,如果没有到达顶部,恢复到初始位置
- // headerContainerView添加到listHeader
- UIView *listHeader = [self listHeaderForListScrollView:self.currentListScrollView];
- if (self.headerContainerView.superview != listHeader) {
- CGRect frame = self.headerContainerView.frame;
- frame.origin.y = 0;
- self.headerContainerView.frame = frame;
- [listHeader addSubview:self.headerContainerView];
- }
-
- // listCollectionView添加到self
- if (self.listCollectionView.superview != self) {
- self.listCollectionView.frame = self.bounds;
- [self insertSubview:self.listCollectionView belowSubview:self.bottomContainerView];
- self->_listCollectionView.headerContainerView = self.headerContainerView;
-
- for (id<GKPageSmoothListViewDelegate> list in self.listDict.allValues) {
- list.listScrollView.contentInset = UIEdgeInsetsMake(self.headerContainerHeight, 0, 0, 0);
- list.listScrollView.contentOffset = CGPointZero;
-
- CGRect frame = list.listView.frame;
- frame.size = self.listCollectionView.bounds.size;
- list.listView.frame = frame;
- }
- self.currentListScrollView.contentOffset = CGPointMake(0, self.currentListPanBeganContentOffsetY);
- }
3、拖拽滑动处理
- - (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture {
- //
- if (panGesture.state == UIGestureRecognizerStateBegan) {
- [self dragBegan];
- }
-
- CGPoint translation = [panGesture translationInView:self.bottomContainerView];
- if (self.isDragScrollView) {
- [self allowScrolling:self.scrollView];
- // 当UIScrollView在最顶部时,处理视图的滑动
- if (self.scrollView.contentOffset.y <= 0) {
- if (translation.y > 0) { // 向下拖拽
- [self forbidScrolling:self.scrollView];
- self.isDragScrollView = NO;
-
- CGRect frame = self.bottomContainerView.frame;
- frame.origin.y += translation.y;
- self.bottomContainerView.frame = frame;
-
- if (!self.isAllowDragScroll) {
- self.scrollView.panGestureRecognizer.enabled = NO;
- self.scrollView.panGestureRecognizer.enabled = YES;
- }
- }
- }
- }else {
- // 根据listScrollView的位置处理bottomContainerView的frame
- CGFloat offsetY = self.scrollView.contentOffset.y;
- CGFloat ceilPointY = self.ceilPointHeight;
-
- if (offsetY <= 0) {
- [self forbidScrolling:self.scrollView];
- if (translation.y > 0) { // 向下拖拽
- CGRect frame = self.bottomContainerView.frame;
- frame.origin.y += translation.y;
- self.bottomContainerView.frame = frame;
- }else if (translation.y < 0 && self.bottomContainerView.frame.origin.y > ceilPointY) { // 向上拖拽
- CGRect frame = self.bottomContainerView.frame;
- frame.origin.y = MAX((self.bottomContainerView.frame.origin.y + translation.y), ceilPointY);
- self.bottomContainerView.frame = frame;
- }
- }else {
- if (translation.y < 0 && self.bottomContainerView.frame.origin.y > ceilPointY) {
- CGRect frame = self.bottomContainerView.frame;
- frame.origin.y = MAX((self.bottomContainerView.frame.origin.y + translation.y), ceilPointY);
- self.bottomContainerView.frame = frame;
- }
-
- if (self.bottomContainerView.frame.origin.y > ceilPointY) {
- [self forbidScrolling:self.scrollView];
- }else {
- [self allowScrolling:self.scrollView];
- }
- }
- }
-
- // 拖拽结束,判断上滑还是下滑并确定是滑动到顶部还是底部
- if (panGesture.state == UIGestureRecognizerStateEnded) {
- CGPoint velocity = [panGesture velocityInView:self.bottomContainerView];
- if (velocity.y < 0) { // 上滑
- if (fabs(self.lastTransitionY) > 5 && self.isDragScrollView == NO) {
- [self dragShowing];
- }else {
- if (self.bottomContainerView.frame.origin.y > (self.ceilPointHeight + self.bottomContainerView.frame.size.height / 2)) {
- [self dragDismiss];
- }else {
- [self dragShowing];
- }
- }
- }else { // 下滑
- if (fabs(self.lastTransitionY) > 5 && self.isDragScrollView == NO && !self.scrollView.isDecelerating) {
- [self dragDismiss];
- }else {
- if (self.bottomContainerView.frame.origin.y > (self.ceilPointHeight + self.bottomContainerView.frame.size.height / 2)) {
- [self dragDismiss];
- }else {
- [self dragShowing];
- }
- }
- }
-
- [self allowScrolling:self.scrollView];
- self.isDragScrollView = NO;
- self.scrollView = nil;
- }
-
- [panGesture setTranslation:CGPointZero inView:self.bottomContainerView];
- self.lastTransitionY = translation.y;
- }
底部拖拽实现基本就这些了,当然还有很多细节,想了解的可以查看具体代码。
1、初始化GKPageSmoothView
- self.smoothView = [[GKPageSmoothView alloc] initWithDataSource:self];
- [self.view addSubView:self.smoothView];
2、初始化headerView和segmentedView
- self.headerView = [UIView new];
- self.segmentedView = [JXCategoryView new];
实现GKPageSmoothViewDataSource代理方法
- #pragma mark - GKPageSmoothViewDataSource
- - (UIView *)headerViewInSmoothView:(GKPageSmoothView *)smoothView {
- return self.headerView;
- }
-
- - (UIView *)segmentedViewInSmoothView:(GKPageSmoothView *)smoothView {
- return self.segmentedView;
- }
-
- - (NSInteger)numberOfListsInSmoothView:(GKPageSmoothView *)smoothView {
- return 2;
- }
-
- - (id<GKPageSmoothListViewDelegate>)smoothView:(GKPageSmoothView *)smoothView initListAtIndex:(NSInteger)index {
- GKDBListView *listView = [GKDBListView new];
- return listView;
- }
列表实现GKPageSmoothListViewDelegate代理方法
- #pragma mark - GKPageSmoothListViewDelegate
- - (UIScrollView *)listScrollView {
- return self.tableView;
- }
-
- - (UIView *)listView {
- return self;
- }
其他常用属性
顶部临界高度
self.smoothView.ceilPointHeight = NAVBAR_HEIGHT;
开启底部悬浮
self.smoothView.bottomHover = YES;
允许底部拖拽
self.smoothView.allowDragBottom = YES;
delegate常用方法代理
- self.smoothView.delegate = self;
-
- /// 列表容器滑动代理
- /// @param smoothView smoothView
- /// @param scrollView containerScrollView
- - (void)smoothView:(GKPageSmoothView *)smoothView scrollViewDidScroll:(UIScrollView *)scrollView;
-
- /// 当前列表滑动代理
- /// @param smoothView smoothView
- /// @param scrollView 当前的列表scrollView
- /// @param contentOffset 转换后的contentOffset
- - (void)smoothView:(GKPageSmoothView *)smoothView listScrollViewDidScroll:(UIScrollView *)scrollView contentOffset:(CGPoint)contentOffset;
-
- /// 开始拖拽代理
- /// @param smoothView smoothView
- - (void)smoothViewDragBegan:(GKPageSmoothView *)smoothView;
-
- /// 结束拖拽代理
- /// @param smoothView smoothView
- /// @param isOnTop 是否通过拖拽滑动到顶部
- - (void)smoothViewDragEnded:(GKPageSmoothView *)smoothView isOnTop:(BOOL)isOnTop;
至此,GKPageSmoothView的介绍已经完成,如果你想了解更多可以查看源码GKPageSmoothView,如果您觉得还不错,可以点个star,您的支持是我最大的动力。
[1]UIScrollView嵌套滚动完美解决方案:仿淘宝、转转首页 https://www.jianshu.com/p/7b1a3feba5a3
[2]iOS仿抖音—评论视图滑动消失 https://www.jianshu.com/p/8a1f174a91e5
- 程序员专栏 扫码关注填加客服 长按识别下方二维码进群
-
近期精彩内容推荐:
在看点这里好文分享给更多人↓↓
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。