【iOS】仿豆瓣電影詳情頁(yè)嵌套滑動(dòng)效果

前言

之前寫的GKPageScrollView已經(jīng)收獲接近900個(gè)star,最近有時(shí)間對(duì)里面的GKPageSmoothView進(jìn)行了優(yōu)化升級(jí),順便實(shí)現(xiàn)了豆瓣電影詳情頁(yè)的效果,本文主要對(duì)GKPageSmoothView的實(shí)現(xiàn)做下簡(jiǎn)單介紹

先看下豆瓣電影的效果圖


豆瓣電影詳情頁(yè)

介紹

GKPageSmoothViewGKPageScrollView的實(shí)現(xiàn)原理不同
GKPageSmoothView主要是通過修改listScrollView的contentInset,然后添加headerView,并在滑動(dòng)到一定程度的時(shí)候?qū)eaderView進(jìn)行位置和布局的轉(zhuǎn)換
GKPageScrollView主要是包含mainTableView和listScrollView,并在合適的位置處理滑動(dòng)沖突

GKPageSmoothView支持以下功能:

  • 支持上下滑動(dòng)、左右滑動(dòng)、手勢(shì)返回等
  • 支持頂部懸停、底部懸停
  • 支持底部懸停拖拽,可實(shí)現(xiàn)豆瓣電影詳情頁(yè)效果
  • 支持如JXCategoryView,JXSegmentedView等的分段控件
  • 可實(shí)現(xiàn)導(dǎo)航欄顏色漸變、頭圖下拉放大等效果
  • 支持主頁(yè)下拉刷新、列表頁(yè)上拉加載

實(shí)現(xiàn)

基本原理可以看這里UIScrollView嵌套滾動(dòng)完美解決方案:仿淘寶、轉(zhuǎn)轉(zhuǎn)首頁(yè)
下面主要講講底部懸停及底部拖拽的實(shí)現(xiàn)

底部懸停

底部懸停的實(shí)現(xiàn)跟頂部懸停的實(shí)現(xiàn)基本一樣,前提是headerView的高度要高于屏幕的高度,這樣當(dāng)headerView下滑到可以懸浮的位置時(shí)將segmentedView添加到GKPageSmoothView上固定,不再跟隨listScrollView滑動(dòng),下面看下主要代碼

// 滑動(dòng)到臨界位置,固定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一起滑動(dòng)
      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];
      }
}

底部拖拽

通過觀察豆瓣詳情頁(yè)發(fā)現(xiàn),拖拽底部時(shí)segmentedView和listScrollView是一起滑動(dòng)的,所以添加了bottomContainerView來處理,并添加拖拽手勢(shì),當(dāng)拖拽bottomContainerView時(shí),讓headerContainerView保持固定不動(dòng),segmentedView和listScrollView根據(jù)bottomContainerView一起滑動(dòng),此時(shí)只需處理拖拽手勢(shì)和listScrollView的滑動(dòng)即可

1、開始拖拽時(shí),處理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;
        
        // 記錄當(dāng)前列表的滑動(dòng)位置
        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、結(jié)束拖拽時(shí),如果沒有到達(dá)頂部,恢復(fù)到初始位置

    // 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、拖拽滑動(dòng)處理

- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture {
    // 
    if (panGesture.state == UIGestureRecognizerStateBegan) {
        [self dragBegan];
    }
    
    CGPoint translation = [panGesture translationInView:self.bottomContainerView];
    if (self.isDragScrollView) {
        [self allowScrolling:self.scrollView];
        // 當(dāng)UIScrollView在最頂部時(shí),處理視圖的滑動(dòng)
        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 {
        // 根據(jù)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];
            }
        }
    }
    
    // 拖拽結(jié)束,判斷上滑還是下滑并確定是滑動(dòng)到頂部還是底部
    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;
}

底部拖拽實(shí)現(xiàn)基本就這些了,當(dāng)然還有很多細(xì)節(jié),想了解的可以查看具體代碼。

使用

1、初始化GKPageSmoothView

self.smoothView = [[GKPageSmoothView alloc] initWithDataSource:self];
[self.view addSubView:self.smoothView];

2、初始化headerView和segmentedView

self.headerView = [UIView new];
self.segmentedView = [JXCategoryView new];

實(shí)現(xiàn)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;
}

列表實(shí)現(xiàn)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;

/// 列表容器滑動(dòng)代理
/// @param smoothView smoothView
/// @param scrollView containerScrollView
- (void)smoothView:(GKPageSmoothView *)smoothView scrollViewDidScroll:(UIScrollView *)scrollView;

/// 當(dāng)前列表滑動(dòng)代理
/// @param smoothView smoothView
/// @param scrollView 當(dāng)前的列表scrollView
/// @param contentOffset 轉(zhuǎn)換后的contentOffset
- (void)smoothView:(GKPageSmoothView *)smoothView listScrollViewDidScroll:(UIScrollView *)scrollView contentOffset:(CGPoint)contentOffset;

/// 開始拖拽代理
/// @param smoothView smoothView
- (void)smoothViewDragBegan:(GKPageSmoothView *)smoothView;

/// 結(jié)束拖拽代理
/// @param smoothView smoothView
/// @param isOnTop 是否通過拖拽滑動(dòng)到頂部
- (void)smoothViewDragEnded:(GKPageSmoothView *)smoothView isOnTop:(BOOL)isOnTop;

結(jié)語(yǔ)

至此,GKPageSmoothView的介紹已經(jīng)完成,如果你想了解更多可以查看源碼GKPageSmoothView,如果您覺得還不錯(cuò),可以點(diǎn)個(gè)star,您的支持是我最大的動(dòng)力。

參考

UIScrollView嵌套滾動(dòng)完美解決方案:仿淘寶、轉(zhuǎn)轉(zhuǎn)首頁(yè)
iOS仿抖音—評(píng)論視圖滑動(dòng)消失

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容