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

2023-12-05

首先查看效果图

就是开始滚动的时候,上面的头部和下面的内容是
一起滚动的,但是当滚动到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
``

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

封装了一个顺滑嵌套滚动的框架 的相关文章

  • 如何将数据从一个视图传递到下一个视图?

    我正在制作一个下载排队系统来下载视频 处理下载的排队代码位于另一个视图控制器中 现在我的问题是如何将下载的 URL 传递到另一个视图而不推送到另一个视图控制器 如下所示 ViewConntroller View ViewConntrolle
  • 如何将 CIFilter 输出到相机视图?

    我刚刚开始使用 Objective C 我正在尝试创建一个简单的应用程序 它显示带有模糊效果的相机视图 我得到了与 AVFoundation 框架一起使用的相机输出 现在 我正在尝试连接 Core 图像框架 但不知道如何连接 Apple 文
  • SwiftUI - 从 NSObject 继承的 ObservableObject 在 iOS 13 中不会更新

    我知道 这是 无法在 iOS XX 中工作 问题之一 但我完全陷入困境 所以我有一个ObservableObject继承自的类NSObject 因为我需要听委托方法UISearchResultsUpdating class SearchBa
  • 使用 NSFileHandle 覆盖数据

    使用 NSFileHandle 使用 truncateFileAtOffset 从文件末尾删除 n 个字符非常容易 void removeCharacters int numberOfCharacters fromEndOfFile NSF
  • iOS 键盘显示后分屏宽度

    我刚刚开始研究 Cordova 应用程序对分屏多任务处理的支持 到目前为止 该应用程序在模拟器中的 iPad 上显示和调整大小都很好 但是当我单击编辑字段并显示软件键盘时 100 宽度的值开始返回整个屏幕 而不是给出的窗口 初始显示 到目前
  • 弱变量中间为零

    弱变量什么时候变为零 weak var backgroundNode SKSpriteNode texture SKTexture image initialBackgroundImage backgroundNode position C
  • 奇怪的父/子NSManagedObjectContext现象

    我创建了两个这样的上下文 create writer MOC privateWriterContext NSManagedObjectContext alloc initWithConcurrencyType NSPrivateQueueC
  • 以编程方式向 UIButton 标签添加阴影

    我试图向按钮标签添加 1px 黑色阴影 但没有成功 我试过这个 self setTitleShadowOffset CGSizeMake 0 1 但我得到 请求非结构或联合中的成员 setTitleShadowOffset 任何建议都会很棒
  • 如何从 iOS 应用程序检测不安全的 wifi 网络

    我想检测我的应用程序中是否存在不安全的 wifi 网络 是否有任何公共 iOS API 可以实现相同的目的 没有记录的 API 可以获取该信息 如果您的应用程序需要通过网络发送和接收敏感数据 您通常应该假设没有安全连接
  • malloc:***错误:已释放对象的校验和不正确 - 对象可能在释放后被修改

    我的 iOS 应用程序有一个大问题 它有时会崩溃 而没有详细的调试错误 堆栈跟踪为空 这是堆栈跟踪中仅有的两行 UIApplicationMain 中的 符号存根 UIHostedTextServiceSession DismissText
  • AFNetworking 上传图片

    我看过一些例子 但我认为我的问题可能出在 PHP 中 我正在尝试使用 AFNetworking 将图像从 iPhone 上传到服务器 这是我的 obj c 代码 IBAction uploadButtonClicked id sender
  • 无需 iPhone 6 Plus 即可预览 iOS 应用程序

    我已经在 Yosemite 中使用 iPhone 6 和 Quicktime 完成了 iOS 应用程序预览 视频 但我无法在 iTunes Connect 中为 iPhone 6 上传它 而且我没有 iPhone 6 设备 有没有办法在没有
  • 如何修复 ReactiveCocoa(带有 CocoaPods)的安装?

    这里是新手 尝试将 ReactiveCocoa 2 2 4 和 CocoaPods 安装到项目中 但在运行时遇到错误 我用过github 上的这个 podspec https github com CocoaPods Specs blob
  • 将语音添加到自定义 UIMenuController

    我创建了一个自定义UIMenuController in a UIWebView但它似乎摆脱了 说出选择 选项UIMenuController在那之后 所有测试设备上的 偏好设置 中都打开了发言选择选项 并且它出现在其他应用程序中 包括非
  • iOS NSURLSession,如何在didCompleteWithError中重试

    我想在我的服务器上尝试一次调用 直到成功为止 我想每 30 秒尝试一次 所以我使用 NSURLSession 进行通话 NSURLSessionDownloadTask task self session downloadTaskWithR
  • 我如何从 iPhone 设备获取电子邮件历史记录..?

    friends 我想从我的 iPhone 访问电子邮件历史记录 并且还希望在收到新邮件时收到通知 如果可能的话 请向我提供源代码片段 Thanks 简而言之 使用任何已记录的 API 都是不可能的
  • 将捕获的图像精确裁剪为 AVCaptureVideoPreviewLayer 中的外观

    我有一个使用 AV Foundation 的照片应用程序 我使用 AVCaptureVideoPreviewLayer 设置了一个预览层 它占据了屏幕的上半部分 因此 当用户尝试拍照时 他们只能看到屏幕上半部分看到的内容 这很好用 但是当用
  • 如何在 Swift 中创建 UIAlertView?

    我一直在努力在 Swift 中创建 UIAlertView 但由于某种原因我无法得到正确的语句 因为我收到此错误 找不到接受提供的 init 重载 论点 我是这样写的 let button2Alert UIAlertView UIAlert
  • Xcode 在代码签名身份中看不到我的开发人员证书

    我续订了 IOS 开发人员证书 从钥匙串中删除了旧证书 然后单击了我的证书 钥匙串中的一切看起来都很正常 我有分发 开发人员 WWDC 证书 每个配置文件看起来都有效 并带有绿色标记 在组织器中的团队和配置文件部分下 但在代码签名身份下的
  • Google Cloud Messaging 显示成功消息但未发送 iOS

    所以我在使用 Google Cloud Messaging 时遇到了一个非常奇怪的问题 我遇到的问题是它正在成功注册设备 并且当发送消息时我会收到来自 Google 的成功消息 但设备永远不会收到任何消息 我从 GCM 得到的消息是 res

随机推荐

  • 嵌入式毕设分享 基于STM32单片机的二轮平衡小车

    文章目录 0 前言 1 简介 2 主要器件 3 实现效果 4 设计原理 4 1 PID算法 4 2 HC SR04超声波模块 4 3 TB6612FNG电机驱动模块 4 4 MPU6050芯片姿态
  • 腾讯视频崩了!“将本增笑”连环暴击!

    一波未平一波又起 滴滴崩了的风波还未平息 腾讯视频就迫不及待的上热搜 语雀崩了 阿里崩了 滴滴崩了 腾讯视频蹦了 疯狂打破大厂技术神话 下一位是谁 12月3日晚 腾讯视频出现网络故障 有网友反馈出现首页无法加载内容 VIP用户看不了会员视频
  • 【车载开发系列】FlashMemory基本概念

    车载开发系列 FlashMemory基本概念 车载开发系列 FlashMemory基本概念 车载开发系列 FlashMemory基本概念 一 FlashMemory的特征 二 常见的FlashMemory 1 NOR FlashMemory
  • 不瞒各位,不安装软件也能操作Xmind文档

    大家好 我是小悟 作为搞技术的一个人群 时不时就要接收产品经理发过来的思维脑图 而此类文档往往是以Xmind编写的 如果你的电脑里面没有安装Xmind的话 不好意思 是打不开这类后缀结尾的文档 打不开的话就看不到 无法洞察产品经理的意思 无
  • 【元胞自动机】元胞自动机交通流模拟仿真【含Matlab源码 1252期】

    博主简介 热爱科研的Matlab仿真开发者 修心和技术同步精进 Matlab项目合作可私信 个人主页 海神之光 代码获取方式 海神之光Matlab王者学习之路 代码获取方式 座右铭 行百里者 半于九十 更多Matlab仿真内容点击 Matl
  • SQL自学通之表达式条件语句与运算

    目录 一 目标 二 表达式条件语句 1 表达式 2 条件 2 1 WHERE 子句 三 运算 1 数值型运算 1 1 加法 1 2 减法 1 3 除法 1 4 乘法 1 5 取模 优先级别 2 比较运算 3 字符操作 LIKE
  • 计算机图形图像技术(图像锐化处理与图像解析)

    一 实验原理 1 拓展Sobel算子锐化 void Sobel Array src Array dst int ddepth int dx int dy int ksize 参数 src 为输入图像 dst 为输出图像 大小和通道数与源图像
  • 计算机网络复习资料

    一 题型 选择题 包括单选和多选 共30分 其中单选每题1分 计20分 多选每题2分 计10分 简答题 每题5分 共20分 分析计算题 共40分 共4题 论述题 本题10分 共1题 二 考试大纲 人工智能 主 计科 补 1 概述 1 互联网
  • 年底了,项目预算怎么创建?9个步骤直接搞定

    如果将项目比作一辆汽车 那么预算就是它的燃料 就像汽车需要汽油一样 项目也需要资金和资源来维持运转 而作为项目经理 应该尽量用最有效的方式规划和使用这些资源 使项目按时交付 项目预算是一项计划 其中详细说明了将花费多少钱 用于哪些方面以及何
  • 【元胞自动机】元胞自动机传染病传播模拟【含Matlab源码 1680期】

    博主简介 热爱科研的Matlab仿真开发者 修心和技术同步精进 Matlab项目合作可私信 个人主页 海神之光 代码获取方式 海神之光Matlab王者学习之路 代码获取方式 座右铭 行百里者 半于九十 更多Matlab仿真内容点击 Matl
  • EPS地形图绘制技巧--快捷键

    如何导入外业点数据 1 打开EPS软件 新建一个工程 如下 2 在 文件 输入输出 调入坐标文件数据 中 调入测量点数据 如下 3 具体步骤如下 1 绘制图形时如何快速捕捉到相应的点 S 在 加线 状态下 输入法切换成英文状态 将鼠标放置在
  • 探索混合模板学习在推荐系统中的应用与个性化推荐效果评估

    在当今数字化时代 推荐系统成为了我们日常生活中不可或缺的一部分 无论是购物网站 社交媒体平台还是音乐应用 推荐系统都能够根据我们的兴趣和偏好 为我们提供个性化的推荐内容 然而 传统的推荐算法在面对用户行为的多样性和数据的海量性时 往往难以取
  • 数据通信技术复习资料

    一 分值分布 1 简答题 60分 共12题 5分一个 含画图 填表形式 2 计算题 35分 共4题 3 论述题 5分 共1题 二 考试范围 1 光纤通信技术占20分 2 数据通信原理主要考1 5章 其中4 5章15分的基本概念 第1章15分
  • 【元胞自动机】元胞自动机交通事故通行【含Matlab源码 1345期】

    博主简介 热爱科研的Matlab仿真开发者 修心和技术同步精进 Matlab项目合作可私信 个人主页 海神之光 代码获取方式 海神之光Matlab王者学习之路 代码获取方式 座右铭 行百里者 半于九十 更多Matlab仿真内容点击 Matl
  • 北京某电视台:持续采购监控易远程维保服务

    一 背景介绍 北京某电视台是一家知名的电视媒体机构 随着业务的发展和技术的不断更新 其对IT设备的管理和维护需求也日益增长 为了确保IT设备的稳定运行和降低运维成本 该电视台决定再次采购监控易1年的远程维保服务 二 解决方案 监控易是一款高
  • Python核心编程之基础内功

    目录 一 语句和语法 1 注释 2 继续 3 多个语句构成代码组
  • 餐饮行业想要做好软文推广的三大技巧,媒介盒子分享

    数字化时代的来临 使越来越多的餐饮品牌和从业者利用软文推广来展示自己的产品 服务和品牌形象 而餐饮行业想要做好软文推广 并不是一味输出内容就行 今天媒介盒子就来和大家分享餐饮行业想要做好软文推广的三大技巧 一 nbsp 软文推广作用 我们首
  • 有哪些好用的视频格式转换方法?推荐这些

    这不快到圣诞节了嘛 为了避免出门人挤人 我和几个朋友商量好了一起在家里聚餐看电影 又能感受节日的氛围 又能避免人挤人 想想就很美好 我这种计划型人格当然要早早计划起来啦 虽然离圣诞节还有一段时间 但是我已经开始在挑选合适的电影 不过下载了几
  • 【元胞自动机】元胞自动机HIV扩散模拟【含Matlab源码 1292期】

    博主简介 热爱科研的Matlab仿真开发者 修心和技术同步精进 Matlab项目合作可私信 个人主页 海神之光 代码获取方式 海神之光Matlab王者学习之路 代码获取方式 座右铭 行百里者 半于九十 更多Matlab仿真内容点击 Matl
  • 封装了一个顺滑嵌套滚动的框架

    首先查看效果图 就是开始滚动的时候 上面的头部和下面的内容是 一起滚动的 但是当滚动到segment 的时候 segment 是悬停 的 下面的tableView是分区的 架构设计 我们设计一个架构 以下面的tablView为主体 上面的内