screenshot.png
#import "ViewController.h"
#import "TableHeaderPic.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)TableHeaderPic *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 创建一个继承 TableHeaderPic的子类视图
self.tableView = [[TableHeaderPic alloc] initWithFrame:self.view.bounds];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
// 创建一张图片,作为tableView底层视图,并进行跟随放大
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width, 200)];
imageView.image = [UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"pic" ofType:@"jpg"]];
_tableView.bottomView = imageView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"UITableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:identifier];
}
cell.textLabel.text = @"123";
return cell;
}
// 实现此效果,必须实现下面的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self.tableView startDetection:scrollView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface TableHeaderPic : UITableView
@property (nonatomic,strong)UIView *bottomView;
- (void)startDetection:(UIScrollView *)scrollView;
@end
#import "TableHeaderPic.h"
#define KSetFrameY(A,B) CGRectMake(A.frame.origin.x,B,A.frame.size.width,A.frame.size.height)
@interface TableHeaderPic ()<UIScrollViewDelegate>
@property (nonatomic,assign)CGRect tempFrame;
@property (nonatomic,assign)CGPoint tempContentOffset;
@property (nonatomic,strong)UIView *headerView;
@end
@implementation TableHeaderPic
// 重写bottom设置方法
- (void)setBottomView:(UIView *)bottomView{
_bottomView = bottomView;
[self addSubview:_bottomView];
self.tempFrame = bottomView.bounds;
self.tableHeaderView = self.headerView;
}
// 初始化headerView
- (UIView *)headerView{
if (!_headerView) {
self.headerView = [[UIView alloc] initWithFrame:_tempFrame];
_headerView.backgroundColor = [UIColor clearColor];
}
return _headerView;
}
// 当视图滚动是改变底层视图的frame
- (void)startDetection:(UIScrollView *)scrollView{
if (scrollView.contentOffset.y > 0) {return;}
CGFloat value = ABS(scrollView.contentOffset.y);
CGFloat scale = (self.tempFrame.size.height + value) / self.tempFrame.size.height;
self.bottomView.transform = CGAffineTransformScale(CGAffineTransformIdentity, scale, scale);
self.bottomView.frame = KSetFrameY(self.bottomView, scrollView.contentOffset.y);
}
@end