#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView * tableVIew;
@property(nonatomic,strong)UIImageView * headImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tableVIew = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
_tableVIew.dataSource = self;
_tableVIew.delegate = self;
[self.view addSubview:_tableVIew];
[_tableVIew registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ID"];
//将ImageView以addSubView的方式加入TableView
_headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -150, self.view.frame.size.width, 150)];
_headImageView.image = [UIImage imageNamed:@"123.jpg"];
//停靠模式
//UIViewContentModeScaleAspectFill 无论坐标怎么变,图片比例一直保持不变
_headImageView.contentMode = UIViewContentModeScaleAspectFill;
//切除多余部分
_headImageView.clipsToBounds = YES;
[_tableVIew addSubview:_headImageView];
//contentInset可以为继承于ScrollView的控件添或减少额外的滑动区域
/*
4个参数分别为相比原有坐标所需添加额外显示区域的偏移量
*/
_tableVIew.contentInset = UIEdgeInsetsMake(150, 0, 0, 0);
//contentInset和contentOffset为对应关系
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"第%lu行",indexPath.row];
return cell;
}
//滚动视图只要滑动即会被调用的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
float offSet = scrollView.contentOffset.y;
if (offSet < - 150) {
//1.图片的顶点始终保持在屏幕的顶点
//得到当前视图的坐标
CGRect rect = _headImageView.frame;
//-150-(-offSet-150)
rect.origin.y = offSet;
//2.更改图片高度
rect.size.height = - offSet;
_headImageView.frame = rect;
}
NSLog(@"%f",offSet);
}