iOS tableView下拉圖片放大
其實這個效果,本質上就是在你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;
}
}
}