• iOS下拉图片放大


    效果图

    开始简单的代码过程

    其实思路很简单 就是 让tableView偏移 一图片的高度,然后在把图片添加到tableView中,然后再监听didScrollView,在里面改变图片的frame

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        self.automaticallyAdjustsScrollViewInsets = NO;
    
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, navH, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - navH) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        /* 设置tableView的偏移量 **/
        _tableView.contentInset = UIEdgeInsetsMake(startImageViewH, 0, 0, 0);
        [self.view addSubview:_tableView];
    
        /*
         * 注意 imageView 的Y 值是-startImageViewH 其实这个值就是 tableView的内容偏移量
         * 如果Y 是0的话它还是会跟着tableView偏移startImageViewH 值
         **/
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -startImageViewH, CGRectGetWidth(self.view.frame), startImageViewH)];
        imageView.image = [UIImage imageNamed:@"1"];
        self.imageView = imageView;
        [self.tableView addSubview:imageView];
    
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
        CGFloat offsetY = scrollView.contentOffset.y;
        NSLog(@"offsetY = %f",offsetY);
    
        /* 计算往下拉的时候偏移量 **/
        if (offsetY < -startImageViewH) {
    
            /* 计算差值 **/
            CGFloat interval = -CGRectGetHeight(self.imageView.frame) - offsetY;
    
            /* imageView 的高度 增加 interval **/
            CGFloat imageViewH = CGRectGetHeight(self.imageView.frame) + interval;
            /* 计算 imageView的 高度增加后的,宽度增加后的值  **/
            CGFloat imageViewW = imageViewH*CGRectGetWidth(self.imageView.frame)/CGRectGetHeight(self.imageView.frame);
            /* 计算 imageView X 的偏移量 **/
            CGFloat imageViewX =self.imageView.frame.origin.x - (imageViewW -CGRectGetWidth(self.imageView.frame))/2;
            /* 计算 imageView Y 的偏移量 **/
            CGFloat imageViewY =self.imageView.frame.origin.y - interval;
            /* 赋值 **/
            self.imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
    
        }
    
    }

    demo地址:https://github.com/lichory/pullDownImageView

    本文借鉴简书作者李重阳_arc的文章,写在这里是为了在自己的博客里做一份备份。

  • 相关阅读:
    VC 中与字符串相关的宏 _T、TEXT,_TEXT、L 的作用(简单明了)
    内存泄露检测工具(25款)
    谈VC++对象模型
    函数调用堆栈分析
    一个跨平台的 C++ 内存泄漏检测器
    网站架构资料收集整理
    分布式缓存系统memcached简介与实践
    反向代理(Reverse Proxy)
    squid和varnish的小结
    ajax简单后台交互
  • 原文地址:https://www.cnblogs.com/ansyxpf/p/5634286.html
Copyright © 2020-2023  润新知