zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

iOS tableView下拉图片放大

ios 图片 放大 下拉 tableView
2023-09-14 09:06:25 时间

事实上这个效果,本质上就是在你tableView下拉 造成offset时候。 保持你顶部图片的y坐标点还停留在下拉时屏幕的顶点(offset), 而图片的长度变为原始的height长度-(offset ) 就达到了下拉放大的效果。

 直接上代码了:

                      1. 首先创建一个UIView作为headerView

            _topView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,MCAPPWidth,80)];

            _tableView.tableHeaderView =_topView;

           2.其次创建一个UIImageView放那张图片 

            _topImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,MCAPPWidth,80)];

            _topImageView.contentMode =UIViewContentModeScaleAspectFill;

            _topImageView.layer.masksToBoundsYES;

            [_topImageViewsd_setImageWithURL:[NSURLURLWithString:nil]placeholderImage:[UIImageimageNamed:@"mian_bg"]];

            [_topViewaddSubview:_topImageView];


           3.最后引入scrollView的代理方法 监听frame的变化

            

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

   if (_tableView == scrollView) {

     CGFloat yOffset = _tableView.contentOffset.y;

    //下拉图片放大

    if (yOffset < 0) {

        _topView.frame = CGRectMake(0, 0, MCAPPWidth80 * Height - yOffset);

        _topImageView.frame = CGRectMake(0, yOffset, MCAPPWidth80 * Height - yOffset);

        NSLog(@"%.2f"_topImageView.y);

    }

    else {

        _topView.frame = CGRectMake(00MCAPPWidth80 * Height - yOffset);

        _topImageView.y = yOffset * 0.65;

        _topImageView.height = 80 * Height - yOffset * 0.65;

    }

   }

}

我这里上拉 图片做的是相对位移 假设仅仅须要下拉变大效果 上拉回原样 须要把else 里的 后两句代码凝视掉, 换成_topImageView.y = _topView.y值保持一样就可以。

我这里的.y是自己封装的方法直接copy会出错哦。