scrollview, tableView nested solutions

scrollview, tableView nested solutions

I searched a lot online but didn't find a solution I liked. I also referred to many designs and made a solution that I think is relatively simple and perfect:

The general idea: Place the scrollview as a container on the outer layer, with the upper part topView and the lower part tableView inside the container. When the tableView scrolls, if the topView is still in the display area, set the y coordinate of the topView so that the topView moves up synchronously.

(Note: If the headerView of the tableView is not set, the tableView and the topView will move up at the same time, which is not the effect I want. Therefore, setting the height of the headerView of the tableView including the height of the topView achieves the perfect effect. See the demo for the specific implementation)

Effect preview:

Please add image description

The core code is the scroll judgment in the parent view and child view.

//Callback of parent view scrolling, used for horizontal scrolling judgment

//父视图滚动的回调,用于横向滚动判断
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
        
    CGFloat placeholderOffset = 0;
    if (self.topView.selectedIndex == 0) {
    
    
        if (self.firstTableView.contentOffset.y > CGRectGetHeight(self.topView.frame) - kItemheight) {
    
    
            placeholderOffset = CGRectGetHeight(self.topView.frame) - kItemheight;
        }else {
    
    
            placeholderOffset = self.firstTableView.contentOffset.y;
        }
        [self.secondTableView setContentOffset:CGPointMake(0, placeholderOffset) animated:NO];
    }else {
    
    
        if (self.secondTableView.contentOffset.y > CGRectGetHeight(self.topView.frame) - kItemheight) {
    
    
            placeholderOffset = CGRectGetHeight(self.topView.frame) - kItemheight;
        }else {
    
    
            placeholderOffset = self.secondTableView.contentOffset.y;
        }
        [self.firstTableView setContentOffset:CGPointMake(0, placeholderOffset) animated:NO];
    }
}


//Callback for subview scrolling, used to determine scrolling in the vertical direction

//子视图滚动的回调,用于竖直方向上滚动判断
- (void)updateTopViewFrame:(UIScrollView *)scrollView{
    
    
    CGFloat placeHolderHeight = CGRectGetHeight(self.topView.frame) - self.topView.itemHeight;
    CGFloat offsetY = scrollView.contentOffset.y;
    
    CGFloat y = 0.0;
    if (offsetY >= 0 && (offsetY <= placeHolderHeight)) {
    
    
        y = -offsetY;
    } else if (offsetY > placeHolderHeight) {
    
    
        y = -placeHolderHeight;
    } else if (offsetY < 0) {
    
    
        y = -offsetY;
    }
    
    [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
    
    
        make.top.offset(y + kNavBarHeight);
    }];
}

Githut demo download address: https://github.com/biyuhuaping/NestScrollView

Guess you like

Origin blog.csdn.net/biyuhuaping/article/details/125898399