其实这个效果,本质上就是在你tableView下拉 造成offset时候, 保持你顶部图片的y坐标点还停留在下拉时屏幕的顶点(offset), 而图片的长度变为原始的height长度-(offset ) 就达到了下拉放大的效果。
直接上代码了:
1. 首先创建一个UIView作为headerView
_topView = [[UIView alloc]initWithFrame:CGRectMake(, , MCAPPWidth, 80)];
_tableView.tableHeaderView = _topView;
_topImageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , MCAPPWidth, 80)];
_topImageView.contentMode = UIViewContentModeScaleAspectFill;
_topImageView.layer.masksToBounds = YES;
[_topImageView sd_setImageWithURL:[NSURL URLWithString:nil] placeholderImage:[UIImage imageNamed:@"mian_bg"]];
[_topView addSubview:_topImageView];
3.最后引入scrollView的代理方法 监听frame的变化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (_tableView == scrollView) {
CGFloat yOffset = _tableView.contentOffset.y;
//下拉图片放大
if (yOffset < ) {
_topView.frame = CGRectMake(, yOffset, MCAPPWidth, 80 * Height - yOffset);
_topImageView.frame = CGRectMake(, yOffset, MCAPPWidth, 80 * Height - yOffset);
NSLog(@"%.2f", _topImageView.y);
}
else {
_topView.frame = CGRectMake(, , MCAPPWidth, 80 * Height - yOffset);
_topImageView.y = yOffset * 0.65;
_topImageView.height = 80 * Height - yOffset * 0.65;
}
}
}