• 一个iOS 列表头部放大工具


    头部放大是移动端开发常用到的UI,这里封装了一个便于使用的头部方放大工具,UITableView 和 UICollectionView 都可以使用

    实现代码

    
    + (VVScrollViewHelper *)initWithScrollView:(UIScrollView *)scrollView
                              headerViewConfig:(VVScrollExtraViewConfig *)headerConfig
    {
        VVScrollViewHelper *scrollViewHelper = [[self alloc] init];
        if(scrollViewHelper){
            
            headerConfig.frontView.clipsToBounds = YES;
            headerConfig.backgroundView.clipsToBounds = YES;
            scrollViewHelper.extraHeaderViewConfig = headerConfig;
            
            CGRect frame = headerConfig.frontView.frame;
            CGPoint origin = frame.origin;
            ///y原点 = 背景视图的原高度 - inset高度
            origin.y = - (CGRectGetHeight(frame) - headerConfig.headerOverHeight);
            frame.origin = origin;
            headerConfig.frontView.frame = frame;
            headerConfig.backgroundView.frame = frame;
            scrollViewHelper.scrollView = scrollView;
            
            scrollViewHelper.contentInsetTop = CGRectGetHeight(frame) - headerConfig.headerOverHeight;
            scrollViewHelper.contentInsetBottom = 0;
            scrollView.contentInset = UIEdgeInsetsMake(scrollViewHelper.contentInsetTop, 0, scrollViewHelper.contentInsetBottom, 0);
            [scrollView setContentOffset:CGPointMake(0, -scrollViewHelper.contentInsetTop) animated:NO];
            scrollView.bouncesZoom = NO;
            [scrollView addSubview:headerConfig.backgroundView];
            [scrollView addSubview:headerConfig.frontView];
            scrollViewHelper.frontViewFrame = headerConfig.frontView.frame;
        }
        return scrollViewHelper;
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        if (self.extraHeaderViewConfig) {
            CGRect frame = self.extraHeaderViewConfig.frontView.frame;
            CGPoint origin = frame.origin;
            CGSize size = frame.size;
            ///下拉,
            BOOL downPull = !(scrollView.contentOffset.y > - (CGRectGetHeight(frame) - self.extraHeaderViewConfig.headerOverHeight));
            ///放大
            BOOL expand = self.extraHeaderViewConfig.headerStyle == VVHeaderBackStyleExpand;
            if (downPull && expand) {
                ///初始偏移量为 (-self.contentInsetTop
                origin.y = scrollView.contentOffset.y;
                size.height = - scrollView.contentOffset.y + self.extraHeaderViewConfig.headerOverHeight;
                frame.origin = origin;
                frame.size = size;
                self.extraHeaderViewConfig.backgroundView.frame = frame;
            }
        }
    
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
                                     (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            ///将背景视图插到最底层
            [scrollView insertSubview:self.extraHeaderViewConfig.backgroundView
                                  atIndex:0];
            ///将顶部内容视图插到第二底层,防止覆盖cell,导致cell不能点击
            [scrollView insertSubview:self.extraHeaderViewConfig.frontView atIndex:1];
        });
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    效果图

    请添加图片描述

    使用方法

    
    - (VVScrollViewHelper *)headerHelper
    {
        if (!_headerHelper) {
            VVScrollExtraViewConfig  *config = [[VVScrollExtraViewConfig alloc] init];
            config.backgroundView = self.imgView;
            config.frontView = self.headerContentView;
            config.headerOverHeight = 40;
            _headerHelper = [VVScrollViewHelper initWithScrollView:self.tableView headerViewConfig:config];
        }
        return _headerHelper;
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        [self.headerHelper scrollViewDidScroll:scrollView];
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    demo
    如果对你有帮助,欢迎star

  • 相关阅读:
    拒绝遗忘:高效的动态规划算法
    [附源码]Python计算机毕业设计 校园疫情防控系统
    [RK3568 Android11]Framework层获取和处理按键事件流程总结
    绘制X-Bar-S和X-Bar-R图,监测过程,计算CPK过程能力指数
    【MySQL进阶之路丨第十七篇(完结)】一文带你精通MySQL运算符
    [CVE-2023-42442]JumpServer 会话录像文件未授权访问漏洞
    【sim-storage-client】SpringBoot集成Minio与本地存储
    数据驱动农业农村现代化-国稻种芯-万祥军:谋定标准化生产
    [CISCN2019 华北赛区 Day1 Web1]Dropbox
    扎实的基础知识+正确的方法是快速阅读源码的关键
  • 原文地址:https://blog.csdn.net/LIUXIAOXIAOBO/article/details/127942692