封装了一个顺滑嵌套滚动的框架

首先查看效果图

就是开始滚动的时候,上面的头部和下面的内容是
一起滚动的,但是当滚动到segment 的时候,segment
是悬停 的,下面的tableView是分区的
请添加图片描述

架构设计

我们设计一个架构,以下面的tablView为主体,上面的内容放在tablView 的contentInset.top范围内,当上滑滚动的时候,如果滑动距离超过了segment的位置,则将segment放在tableView的父视图上,这样达到了一个悬停的效果,看上去是悬停了,其实是segment的父视图从tableView 变成了tableView的父视图,下拉的时候同样如此,当下拉达到临界便宜量的时候,segment的就从tableView的父视图变成了tablView,这样就跟着tableView滚动了,达到了顺畅嵌套滚动的效果

代码

`
@interface LBPageSmoothView()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate>

@property (nonatomic, weak) id dataSource;
@property (nonatomic, strong) GKPageSmoothCollectionView *listCollectionView;
@property (nonatomic, strong) NSMutableDictionary <NSNumber *, id> *listDict;
@property (nonatomic, strong) NSMutableDictionary <NSNumber *, UIView *> *listHeaderDict;

@property (nonatomic, assign) LBPageSmoothHoverType hoverType;

@property (nonatomic, strong) UIView *headerContainerView;
@property (nonatomic, weak) UIView *headerView;
@property (nonatomic, weak) UIView *segmentedView;
@property (nonatomic, weak) UIScrollView *currentListScrollView;

@property (nonatomic, strong) UIView *bottomContainerView;

@property (nonatomic, assign) BOOL syncListContentOffsetEnabled;
@property (nonatomic, assign) CGFloat currentHeaderContainerViewY;

@property (nonatomic, assign) CGFloat headerContainerHeight;
@property (nonatomic, assign) CGFloat headerHeight;
@property (nonatomic, assign) CGFloat segmentedHeight;
@property (nonatomic, assign) CGFloat currentListInitializeContentOffsetY;

@property (nonatomic, assign) NSInteger currentIndex;

@property (nonatomic, assign) BOOL isLoaded;

@property (nonatomic, strong) UIPanGestureRecognizer *panGesture;

@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, assign) BOOL isDragScrollView;
@property (nonatomic, assign) CGFloat lastTransitionY;
@property (nonatomic, assign) BOOL isOnTop;

@property (nonatomic, assign) CGFloat currentListPanBeganContentOffsetY;
@property (nonatomic, assign) BOOL originBounces;
@property (nonatomic, assign) BOOL originShowsVerticalScrollIndicator;

@end

@implementation LBPageSmoothView

  • (instancetype)initWithDataSource:(id)dataSource {
    if (self = [super initWithFrame:CGRectZero]) {
    self.dataSource = dataSource;
    _listDict = [NSMutableDictionary dictionary];
    _listHeaderDict = [NSMutableDictionary dictionary];
    _ceilPointHeight = 0;

      [self addSubview:self.listCollectionView];
      [self addSubview:self.headerContainerView];
      [self refreshHeaderView];
    

    }
    return self;
    }

  • (void)dealloc {
    for (id listItem in self.listDict.allValues) {
    [listItem.listScrollView removeObserver:self forKeyPath:@“contentOffset”];
    }
    }

  • (void)layoutSubviews {
    [super layoutSubviews];

    if (self.listCollectionView.superview == self) {
    self.listCollectionView.frame = self.bounds;
    }else {
    CGRect frame = self.listCollectionView.frame;
    frame.origin.y = self.segmentedHeight;
    frame.size.height = self.bottomContainerView.frame.size.height - self.segmentedHeight;
    self.listCollectionView.frame = frame;
    }
    }

  • (void)refreshHeaderView {
    [self loadHeaderAndSegmentedView];

    __weak __typeof(self) weakSelf = self;
    [self refreshWidthCompletion:^(CGSize size) {
    __strong __typeof(weakSelf) self = weakSelf;
    CGRect frame = self.headerContainerView.frame;
    if (CGRectEqualToRect(frame, CGRectZero)) {
    frame = CGRectMake(0, 0, size.width, self.headerContainerHeight);
    }else {
    frame.size.height = self.headerContainerHeight;
    }
    self.headerContainerView.frame = frame;

      self.headerView.frame = CGRectMake(0, 0, size.width, self.headerHeight);
      self.segmentedView.frame =  CGRectMake(0, self.headerHeight, size.width, self.segmentedHeight);
      
      for (id<LBPageSmoothListViewDelegate> list in self.listDict.allValues) {
          list.listScrollView.contentInset = UIEdgeInsetsMake(self.headerContainerHeight, 0, 0, 0);
      }
      
      if (self.isBottomHover) {
          self.bottomContainerView.frame = CGRectMake(0, size.height - self.segmentedHeight, size.width, size.height - self.ceilPointHeight);
          
          if (self.headerHeight > size.height) {
              self.segmentedView.frame = CGRectMake(0, 0, size.width, self.segmentedHeight);
              [self.bottomContainerView addSubview:self.segmentedView];
          }
      }
    

    }];
    }

  • (void)updateSegmentHeight:(CGFloat)height
    {
    CGFloat offset = height - self.segmentedHeight;
    self.segmentedHeight = height;
    self.headerContainerHeight = self.headerHeight + self.segmentedHeight;
    for (id list in self.listDict.allValues) {
    CGFloat currentOffset = list.listScrollView.contentOffset.y;
    list.listScrollView.contentInset = UIEdgeInsetsMake(self.headerContainerHeight, 0, 0, 0);
    CGPoint tooffset = CGPointMake(0, currentOffset - offset);
    list.listScrollView.contentOffset = tooffset;
    }

    self.segmentedView.frame = CGRectMake(CGRectGetMinX(self.segmentedView.frame), CGRectGetMinY(self.segmentedView.frame), CGRectGetWidth(self.segmentedView.frame), height);

    self.headerContainerView.frame = CGRectMake(CGRectGetMinX(self.headerContainerView.frame), CGRectGetMinY(self.headerContainerView.frame), CGRectGetWidth(self.headerContainerView.frame), self.headerContainerHeight);
    for (UIView *listHeaderView in self.listHeaderDict.allValues) {
    listHeaderView.frame = CGRectMake(0, -self.headerContainerHeight, self.bounds.size.width, self.headerContainerHeight);
    }
    }

  • (void)reloadData {
    self.currentListScrollView = nil;
    self.currentIndex = self.defaultSelectedIndex;
    self.syncListContentOffsetEnabled = NO;
    self.currentHeaderContainerViewY = 0;
    self.isLoaded = YES;

    [self.listHeaderDict removeAllObjects];

    for (id list in self.listDict.allValues) {
    [list.listScrollView removeObserver:self forKeyPath:@“contentOffset”];
    [list.listView removeFromSuperview];
    }
    [_listDict removeAllObjects];

    __weak __typeof(self) weakSelf = self;
    [self refreshWidthCompletion:^(CGSize size) {
    __strong __typeof(weakSelf) self = weakSelf;
    [self.listCollectionView setContentOffset:CGPointMake(size.width * self.currentIndex, 0) animated:NO];
    [self.listCollectionView reloadData];
    }];
    }

  • (void)scrollToOriginalPoint {
    [self.currentListScrollView setContentOffset:CGPointMake(0, -self.headerContainerHeight) animated:YES];
    }

  • (void)scrollToCriticalPoint {
    [self.currentListScrollView setContentOffset:CGPointMake(0, -(self.segmentedHeight+self.ceilPointHeight)) animated:YES];
    }

  • (void)showingOnTop {
    if (self.bottomContainerView.isHidden) return;
    [self dragBegan];
    [self dragShowing];
    }

  • (void)showingOnBottom {
    if (self.bottomContainerView.isHidden) return;
    [self dragDismiss];
    }

  • (void)setBottomHover:(BOOL)bottomHover {
    _bottomHover = bottomHover;

    if (bottomHover) {
    __weak __typeof(self) weakSelf = self;
    [self refreshWidthCompletion:^(CGSize size) {
    __strong __typeof(weakSelf) self = weakSelf;
    self.bottomContainerView.frame = CGRectMake(0, size.height - self.segmentedHeight, size.width, size.height - self.ceilPointHeight);
    [self addSubview:self.bottomContainerView];

          if (self.headerHeight > size.height) {
              self.segmentedView.frame = CGRectMake(0, 0, size.width, self.segmentedHeight);
              [self.bottomContainerView addSubview:self.segmentedView];
          }
      }];
    

    }else {
    [self.bottomContainerView removeFromSuperview];
    }
    }

  • (void)setAllowDragBottom:(BOOL)allowDragBottom {
    _allowDragBottom = allowDragBottom;

    if (self.bottomHover) {
    if (allowDragBottom) {
    [self.bottomContainerView addGestureRecognizer:self.panGesture];
    }else {
    [self.bottomContainerView removeGestureRecognizer:self.panGesture];
    }
    }
    }

#pragma mark - UICollectionViewDataSource

  • (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.isLoaded ? 1 : 0;
    }

  • (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.dataSource numberOfListsInSmoothView:self];
    }

  • (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:GKPageSmoothViewCellID forIndexPath:indexPath];
    id list = self.listDict[@(indexPath.item)];
    if (list == nil) {
    list = [self.dataSource smoothView:self initListAtIndex:indexPath.item];
    _listDict[@(indexPath.item)] = list;
    [list.listView setNeedsLayout];
    [list.listView layoutIfNeeded];

      UIScrollView *listScrollView = list.listScrollView;
      if ([listScrollView isKindOfClass:[UITableView class]]) {
          ((UITableView *)listScrollView).estimatedRowHeight = 0;
          ((UITableView *)listScrollView).estimatedSectionHeaderHeight = 0;
          ((UITableView *)listScrollView).estimatedSectionFooterHeight = 0;
      }
      if (@available(iOS 11.0, *)) {
          listScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
      }
      
      if (CGSizeEqualToSize(listScrollView.contentSize, CGSizeZero)) {
          listScrollView.contentSize = CGSizeMake(listScrollView.contentSize.width, self.bounds.size.height);
      }
      
      if (!self.isOnTop) {
          listScrollView.contentInset = UIEdgeInsetsMake(self.headerContainerHeight, 0, self.bottomInset, 0);
          self.currentListInitializeContentOffsetY = -listScrollView.contentInset.top + MIN(-self.currentHeaderContainerViewY, (self.headerHeight - self.ceilPointHeight));
          listScrollView.contentOffset = CGPointMake(0, self.currentListInitializeContentOffsetY);
      }
      UIView *listHeader = [[UIView alloc] initWithFrame:CGRectMake(0, -self.headerContainerHeight, self.bounds.size.width, self.headerContainerHeight)];
      [listScrollView addSubview:listHeader];
      
      if (!self.isOnTop && self.headerContainerView.superview == nil) {
          [listHeader addSubview:self.headerContainerView];
      }
      self.listHeaderDict[@(indexPath.item)] = listHeader;
      [listScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
      // bug fix #69 修复首次进入时可能出现的headerView无法下拉的问题
      [listScrollView setContentOffset:listScrollView.contentOffset];
    

    }
    for (id listItem in self.listDict.allValues) {
    listItem.listScrollView.scrollsToTop = (listItem == list);
    }

    UIView *listView = list.listView;
    if (listView != nil && listView.superview != cell.contentView) {
    for (UIView *view in cell.contentView.subviews) {
    [view removeFromSuperview];
    }
    listView.frame = cell.contentView.bounds;
    [cell.contentView addSubview:listView];
    }
    return cell;
    }

  • (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.listCollectionView.bounds.size;
    }

  • (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];
    }

  • (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.panGesture.enabled = NO;
    }

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([self.delegate respondsToSelector:@selector(smoothView:scrollViewDidScroll:)]) {
    [self.delegate smoothView:self scrollViewDidScroll:scrollView];
    }

    NSInteger index = scrollView.contentOffset.x / scrollView.bounds.size.width;

    NSInteger ratio = (int)scrollView.contentOffset.x % (int)scrollView.bounds.size.width;

    if (!self.isOnTop) {
    UIScrollView *listScrollView = self.listDict[@(index)].listScrollView;
    if (index != self.currentIndex && ratio == 0 && !(scrollView.isDragging || scrollView.isDecelerating) && listScrollView.contentOffset.y <= -(self.segmentedHeight + self.ceilPointHeight)) {
    [self horizontalScrollDidEndAtIndex:index];
    }else {
    // 左右滚动的时候,把headerContainerView添加到self,达到悬浮的效果
    if (self.headerContainerView.superview != self) {
    CGRect frame = self.headerContainerView.frame;
    frame.origin.y = self.currentHeaderContainerViewY;
    self.headerContainerView.frame = frame;
    [self addSubview:self.headerContainerView];
    }
    }
    }

    if (index != self.currentIndex && ratio == 0) {
    self.currentIndex = index;
    }
    }

  • (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (!decelerate) {
    NSInteger index = scrollView.contentOffset.x / scrollView.bounds.size.width;
    [self horizontalScrollDidEndAtIndex:index];
    }
    self.panGesture.enabled = YES;
    }

  • (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / scrollView.bounds.size.width;
    [self horizontalScrollDidEndAtIndex:index];
    self.panGesture.enabled = YES;
    }

  • (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    // 修复快速闪烁问题
    self.currentIndex = scrollView.contentOffset.x / scrollView.bounds.size.width;
    self.currentListScrollView = self.listDict[@(self.currentIndex)].listScrollView;
    }

#pragma mark - KVO

  • (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@“contentOffset”]) {
    UIScrollView *scrollView = (UIScrollView *)object;
    if (scrollView != nil) {
    [self listScrollViewDidScroll:scrollView];
    }
    }else {
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
    }

#pragma mark - Gesture

  • (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture {
    if (panGesture.state == UIGestureRecognizerStateBegan) {
    if ([self.delegate respondsToSelector:@selector(smoothViewDragBegan:)]) {
    [self.delegate smoothViewDragBegan:self];
    }
    [self dragBegan];

      // 记录scrollView的某些属性
      self.originBounces = self.scrollView.bounces;
      self.originShowsVerticalScrollIndicator = self.scrollView.showsVerticalScrollIndicator;
      
      // bug fix #47,当UIScrollView向下滚动的时候,向下拖拽完成手势操作导致的错乱问题
      if (self.currentListScrollView.isDecelerating) {
          [self.currentListScrollView setContentOffset:self.currentListScrollView.contentOffset animated:NO];
      }
    

    }

    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 {
    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;
    }

#pragma mark - UIGestureRecognizerDelegate

  • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (gestureRecognizer == self.panGesture) {
    UIView *touchView = touch.view;
    while (touchView != nil) {
    if (touchView == self.currentListScrollView) {
    self.scrollView = (UIScrollView *)touchView;
    self.isDragScrollView = YES;
    break;
    }else if (touchView == self.bottomContainerView) {
    self.isDragScrollView = NO;
    break;
    }
    touchView = (UIView *)[touchView nextResponder];
    }
    }
    return YES;
    }

  • (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    // 左右滑动时禁止上下滑动
    CGPoint transition = [gestureRecognizer translationInView:gestureRecognizer.view];
    if (transition.x != 0) return NO;
    return YES;
    }

  • (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if (gestureRecognizer == self.panGesture) {
    if (otherGestureRecognizer == self.scrollView.panGestureRecognizer) {
    return YES;
    }
    }
    return NO;
    }

#pragma mark - Private Methods

  • (void)listScrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.listCollectionView.isDragging || self.listCollectionView.isDecelerating) return;

    if (self.isOnTop) { // 在顶部时无需处理headerView
    // 取消scrollView下滑时的弹性效果
    // buf fix #47,iOS12及以下系统isDragging会出现不准确的情况,所以这里改为用isTracking判断
    if (self.isAllowDragScroll && (scrollView.isTracking || scrollView.isDecelerating)) {
    if (scrollView.contentOffset.y < 0) {
    scrollView.contentOffset = CGPointZero;
    }
    }

      if ([self.delegate respondsToSelector:@selector(smoothView:listScrollViewDidScroll:contentOffset:)]) {
          [self.delegate smoothView:self listScrollViewDidScroll:scrollView contentOffset:scrollView.contentOffset];
      }
    

    }else { // 不在顶部,通过列表scrollView滑动确定悬浮位置
    NSInteger listIndex = [self listIndexForListScrollView:scrollView];
    if (listIndex != self.currentIndex) return;
    self.currentListScrollView = scrollView;
    CGFloat contentOffsetY = scrollView.contentOffset.y + self.headerContainerHeight;

      if (contentOffsetY < (self.headerHeight - self.ceilPointHeight)) {
          self.hoverType = LBPageSmoothHoverTypeNone;
          self.syncListContentOffsetEnabled = YES;
          self.currentHeaderContainerViewY = -contentOffsetY;
          for (id<LBPageSmoothListViewDelegate> list in self.listDict.allValues) {
              if (list.listScrollView != scrollView) {
                  [list.listScrollView setContentOffset:scrollView.contentOffset animated:NO];
              }
          }
          UIView *listHeader = [self listHeaderForListScrollView:scrollView];
          if (self.headerContainerView.superview != listHeader) {
              CGRect frame = self.headerContainerView.frame;
              frame.origin.y = 0;
              self.headerContainerView.frame = frame;
              [listHeader addSubview:self.headerContainerView];
          }
          
          if (self.isControlVerticalIndicator && self.ceilPointHeight != 0) {
              self.currentListScrollView.showsVerticalScrollIndicator = NO;
          }
          
          if (self.isBottomHover) {
              if (contentOffsetY < (self.headerContainerHeight - self.frame.size.height)) {
                  self.hoverType = LBPageSmoothHoverTypeBottom;
                  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 {
                  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];
                  }
              }
          }
      }else {
          self.hoverType = LBPageSmoothHoverTypeTop;
          if (self.headerContainerView.superview != self) {
              CGRect frame = self.headerContainerView.frame;
              frame.origin.y = - (self.headerHeight - self.ceilPointHeight);
              self.headerContainerView.frame = frame;
              [self addSubview:self.headerContainerView];
          }
          
          if (self.isControlVerticalIndicator) {
              self.currentListScrollView.showsVerticalScrollIndicator = YES;
          }
          
          if (self.syncListContentOffsetEnabled) {
              self.syncListContentOffsetEnabled = NO;
              self.currentHeaderContainerViewY = -(self.headerHeight - self.ceilPointHeight);
              for (id<LBPageSmoothListViewDelegate> listItem in self.listDict.allValues) {
                  if (listItem.listScrollView != scrollView) {
                      [listItem.listScrollView setContentOffset:CGPointMake(0, -(self.segmentedHeight + self.ceilPointHeight)) animated:NO];
                  }
              }
          }
      }
      CGPoint contentOffset = CGPointMake(scrollView.contentOffset.x, contentOffsetY);
      if ([self.delegate respondsToSelector:@selector(smoothView:listScrollViewDidScroll:contentOffset:)]) {
          [self.delegate smoothView:self listScrollViewDidScroll:scrollView contentOffset:contentOffset];
      }
    

    }
    }

  • (void)loadHeaderAndSegmentedView {
    self.headerView = [self.dataSource headerViewInSmoothView:self];
    self.segmentedView = [self.dataSource segmentedViewInSmoothView:self];
    [self.headerContainerView addSubview:self.headerView];
    [self.headerContainerView addSubview:self.segmentedView];

    self.headerHeight = self.headerView.bounds.size.height;
    self.segmentedHeight = self.segmentedView.bounds.size.height;
    self.headerContainerHeight = self.headerHeight + self.segmentedHeight;
    }

  • (void)refreshWidthCompletion:(void(^)(CGSize size))completion {
    if (self.bounds.size.width == 0) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    !completion ? : completion(self.bounds.size);
    });
    }else {
    !completion ? : completion(self.bounds.size);
    }
    }

  • (void)horizontalScrollDidEndAtIndex:(NSInteger)index {
    self.currentIndex = index;
    UIView *listHeader = self.listHeaderDict[@(index)];
    UIScrollView *listScrollView = self.listDict[@(index)].listScrollView;
    self.currentListScrollView = listScrollView;
    if (self.isOnTop) return;
    if (listHeader != nil && listScrollView.contentOffset.y <= -(self.segmentedHeight + self.ceilPointHeight)) {
    for (id listItem in self.listDict.allValues) {
    listItem.listScrollView.scrollsToTop = (listItem.listScrollView == listScrollView);
    }
    CGRect frame = self.headerContainerView.frame;
    frame.origin.y = 0;
    self.headerContainerView.frame = frame;
    if (self.headerContainerView.superview != listHeader) {
    [listHeader addSubview:self.headerContainerView];
    }
    }
    }

  • (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 numberOfListsInSmoothView:self];
    if (count <= 0 || index >= count) return;

    id list = self.listDict[@(index)];
    if (list && [list respondsToSelector:@selector(listViewDidAppear)]) {
    [list listViewDidAppear];
    }
    }

  • (void)listDidDisappear:(NSInteger)index {
    NSUInteger count = [self.dataSource numberOfListsInSmoothView:self];
    if (count <= 0 || index >= count) return;

    id list = self.listDict[@(index)];
    if (list && [list respondsToSelector:@selector(listViewDidDisappear)]) {
    [list listViewDidDisappear];
    }
    }

  • (void)allowScrolling:(UIScrollView *)scrollView {
    scrollView.bounces = self.originBounces;
    scrollView.showsVerticalScrollIndicator = self.originShowsVerticalScrollIndicator;
    }

  • (void)forbidScrolling:(UIScrollView *)scrollView {
    scrollView.contentOffset = CGPointZero;
    scrollView.bounces = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    }

  • (void)dragBegan {
    self.isOnTop = YES;
    [self setupShowingLayout];
    }

  • (void)dragDismiss {
    [UIView animateWithDuration:0.25 animations:^{
    CGRect frame = self.bottomContainerView.frame;
    frame.origin.y = self.frame.size.height - self.segmentedHeight;
    self.bottomContainerView.frame = frame;
    } completion:^(BOOL finished) {
    [self setupDismissLayout];

      self.isOnTop = NO;
      if ([self.delegate respondsToSelector:@selector(smoothViewDragEnded:isOnTop:)]) {
          [self.delegate smoothViewDragEnded:self isOnTop:self.isOnTop];
      }
    

    }];
    }

  • (void)dragShowing {
    [UIView animateWithDuration:0.25 animations:^{
    CGRect frame = self.bottomContainerView.frame;
    frame.origin.y = self.ceilPointHeight;
    self.bottomContainerView.frame = frame;
    } completion:^(BOOL finished) {
    if ([self.delegate respondsToSelector:@selector(smoothViewDragEnded:isOnTop:)]) {
    [self.delegate smoothViewDragEnded:self isOnTop:self.isOnTop];
    }
    }];
    }

  • (void)setupShowingLayout {
    // 将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<LBPageSmoothListViewDelegate> 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;
      }
    

    }
    }

  • (void)setupDismissLayout {
    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];
    }

    if (self.listCollectionView.superview != self) {
    self.listCollectionView.frame = self.bounds;
    [self insertSubview:self.listCollectionView belowSubview:self.bottomContainerView];
    self->_listCollectionView.headerContainerView = self.headerContainerView;

      for (id<LBPageSmoothListViewDelegate> 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);
    

    }
    }

#pragma mark - Getter

  • (UICollectionView *)listCollectionView {
    if (!_listCollectionView) {
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    _listCollectionView = [[GKPageSmoothCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _listCollectionView.dataSource = self;
    _listCollectionView.delegate = self;
    _listCollectionView.pagingEnabled = YES;
    _listCollectionView.bounces = NO;
    _listCollectionView.showsHorizontalScrollIndicator = NO;
    _listCollectionView.scrollsToTop = NO;
    [_listCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:GKPageSmoothViewCellID];
    if (@available(iOS 11.0, *)) {
    _listCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    if (@available(iOS 10.0, *)) {
    _listCollectionView.prefetchingEnabled = NO;
    }
    _listCollectionView.backgroundColor = [UIColor clearColor];
    _listCollectionView.headerContainerView = self.headerContainerView;
    }
    return _listCollectionView;
    }

  • (UIView *)headerContainerView {
    if (!_headerContainerView) {
    _headerContainerView = [UIView new];
    }
    return _headerContainerView;
    }

  • (UIView *)bottomContainerView {
    if (!_bottomContainerView) {
    _bottomContainerView = [UIView new];
    _bottomContainerView.backgroundColor = UIColor.whiteColor;
    }
    return _bottomContainerView;
    }

  • (UIPanGestureRecognizer *)panGesture {
    if (!_panGesture) {
    _panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    _panGesture.delegate = self;
    }
    return _panGesture;
    }

@end
``

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/213016.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

大数据湖项目建设方案:文档全文101页,附下载

关键词&#xff1a;大数据解决方案&#xff0c;数据湖解决方案&#xff0c;数据治理解决方案&#xff0c;数据中台解决方案 一、大数据湖建设思路 1、明确目标和定位&#xff1a;明确大数据湖的目标和定位是整个项目的基础&#xff0c;这可以帮助我们确定项目的内容、规模、所…

P3 Linux应用编程:系统调用与库函数

前言 &#x1f3ac; 个人主页&#xff1a;ChenPi &#x1f43b;推荐专栏1: 《C_ChenPi的博客-CSDN博客》✨✨✨ &#x1f525; 推荐专栏2: 《Linux C应用编程&#xff08;概念类&#xff09;_ChenPi的博客-CSDN博客》✨✨✨ &#x1f6f8;推荐专栏3: ​​​​​​《 链表_Chen…

Kubernetes入门学习(下)

Kubernetes入门学习&#xff08;下&#xff09; 文章目录 Kubernetes入门学习&#xff08;下&#xff09;运行有状态的应用ConfigMap与SecretConfigMapSecret 卷(Volume)StatefulSet(有状态应用集)Headless Service(无头服务)Mysql主从复制Port-forward端口转发Helm参考 运行有…

Java中异常处理顺序和全局异常处理器

异常处理顺序 我们直接通过代码看下Java中异常的处理顺序。 数组越界异常属于运行时异常&#xff0c;被捕捉后就停止了&#xff0c;打印结果为数组越界了。 Test public void test2(){int[] arr new int[4];try{System.out.println(arr[5]);}catch (ArrayIndexOutOfBoundsE…

2023.12.2 关于 Spring AOP 详解

目录 Spring AOP Spring AOP 常见使用场景 AOP 组成 切面&#xff08;类&#xff09; 切点&#xff08;方法&#xff09; 通知 ​编辑 前置通知&#xff08;Before&#xff09; 后置通知&#xff08;After&#xff09; 返回通知&#xff08;AfterReturning&#xff0…

【接口测试】Apifox实用技巧干货分享

前言 不知道有多少人和我有着这样相似的经历&#xff1a;从写程序只要不报错就不测试&#x1f60a;&#xff0c;到写了程序若是有bug就debug甚至写单元测试&#xff0c;然后到了真实开发场景&#xff0c;大哥和你说&#xff0c;你负责的功能模块的所有接口写完要测试一遍无误在…

C# 使用HtmlAgilityPack解析提取HTML内容

写在前面 HtmlAgilityPack是一个HTML解析类库&#xff0c;日常用法就是爬虫获取到内容后&#xff0c;先用XPath获取目标节点&#xff0c;再用正则进行匹配&#xff1b;使用XPath的目的主要是将目标节点或内容限定在一个较小的范围&#xff0c;如果一上来就用正则那效率肯定不…

python——进程常用功能

Python的multiprocessing模块提供了强大的并行处理能力&#xff0c;以下是几个功能的详细解释&#xff1a; join(): 在multiprocessing中&#xff0c;join方法用于阻塞主进程直到指定的进程终止。这对于确保所有子进程在程序结束前完成其工作是很有用的。deamon(): 在multipro…

讲一讲redis的使用

Redis&#xff08;Remote Dictionary Server&#xff09;是一个开源的内存数据库系统&#xff0c;它提供了高性能、支持多种数据结构的存储和操作&#xff0c;被广泛应用于缓存、消息队列、计数器、实时分析等场景。以下是Redis的使用详解&#xff0c;涵盖了基本概念、数据结构…

目标检测常用评价指标

1 基本概念 1.1 IOU(Intersection over Union) 1.2 TP TN FP FN 2. 各种率 3. PR曲线 4. mAP的计算 4.1 AP的计算 4.2 mAP 4.3 mAP0.5和mAP0.5:0.95 1.1 IOU(Intersection over Union) 1.2 TP TN FP FN TP(Truth Positive)&#xff1a; 预测正类&#xff0c;实际正类&#x…

2022CVPR(PoseC3D):Revisiting Skeleton-based Action Recognition

Revisiting Skeleton-based Action Recognition 摘要1、引言2、相关工作3、框架3.1. 姿势提取的良好实践3.2.从2D姿势到3D热图体积3.3.基于骨架的动作识别的3D-CNN 4、实验4.2.姿势提取4.3. 3D热图体积的预处理4.4.与GCN的比较4.5. RGBPose-SlowFast4.6.与最先进的比较 5、结论…

糟了,数据库崩了,又好像没崩

前言 2023 年某一天周末&#xff0c;新手程序员小明因为领导安排的一个活来到公司加班&#xff0c;小明三下五除二&#xff0c;按照领导要求写了一个跑批的数据落库任务在测试环境执行 &#xff0c;突然间公司停电了&#xff0c;小明大惊&#xff0c;“糟了&#xff0c;MySQL …

wordpress建站优化加速教程-Redis加速

这篇文章适合宝塔面板&#xff0c;在宝塔面板安装 Redis 实现网站加速&#xff08; Redis是一个高性能的key-value数据库(PHP连接redis&#xff0c;需PHP设置中安装redis扩展) &#xff09;。对在word press网站有着明显的加速效果。关于Redis具体说明请自己百度&#xff0c;…

30岁左右的简历模板精选7篇

30岁左右是职业发展的关键时期&#xff0c;一份出色的简历能带来更多机会。本文精选了7篇适合30岁左右求职者的专业简历案例&#xff0c;无论您是寻找晋升、转行还是新的职业挑战&#xff0c;都能从中借鉴灵感&#xff0c;打造一份令人印象深刻的简历。 30岁左右的简历模板下载…

Git 配置文件(.gitignore)

前言 在使用 Git 分布式版本控制系统的时候&#xff0c;有些文件如&#xff1a;数据库的一些配置文件&#xff0c;我们不想让这类文件在远程仓库让 Git 来管理&#xff0c;不想让别人看到&#xff0c;此时就可以自己在 Git 仓库目录下创建 / 在远程仓库创建的时候就配置好 .git…

队列顺序存储(详解)

队列是一种常见的数据结构&#xff0c;它是一种先进先出&#xff08;First-In-First-Out, FIFO&#xff09;的线性表。在队列中&#xff0c;数据元素按照插入的顺序排列&#xff0c;最先插入的元素在队列的前面&#xff0c;最后插入的元素在队列的后面。类比生活中排队购物的情…

调试GMS应用,报错“此设备未获得play保护机制认证”问题解决

不少同学在调试GMS相关应用时&#xff0c;需登录Google账号&#xff0c;有时会弹出如下通知。 Google登录界面也会出现如下提示 这个报错的原因是设备未通过Google认证&#xff0c;google服务器未配置荣耀设备的型号白名单导致 国内网页有一些指导方法在鸿蒙\荣耀的设备上消除这…

语言模型文本处理基石:Tokenizer简明概述

编者按&#xff1a;近年来&#xff0c;人工智能技术飞速发展&#xff0c;尤其是大型语言模型的问世&#xff0c;让 AI 写作、聊天等能力有了质的飞跃。如何更好地理解和利用这些生成式 AI&#xff0c;成为许多开发者和用户关心的问题。 今天&#xff0c;我们推出的这篇文章有助…

Linux环境下 make/makefile、文件时间属性 详解!!!

1.项目自动化构建工具make/makefile 1.为什么要有make/makefile 我们先写一个简单的代码&#xff0c;然后编译生成一个可执行程序&#xff0c;下面的内容我们需要知道gcc识和编译链接的一些知识&#xff0c;不清楚的朋友们可以点这里http://t.csdnimg.cn/0QvL8 我们知道要想生…

Python爬虫:通过js逆向分析某翻译网站的原理

Python爬虫&#xff1a;通过js逆向分析某翻译网站的原理 1. 网站实现原理2. 抓取接口3. 参考代码和运行结果 1. 网站实现原理 首先&#xff0c;说一下爬取的网站&#xff1a;百度翻译。网站实现翻译的效果是通过接口实现的&#xff0c;也就是各位听到的ajax技术(只需要更换对应…