#define ScreenW [UIScreen mainScreen].bounds.size.width#define ScreenH [UIScreen mainScreen].bounds.size.height#define XGHeaderImageH 200
#import "ViewController.h"@interface ViewController (){
UITableView *table;
}
@property (nonatomic, weak) UIView *headerView;
@property (nonatomic, weak) UIImageView *headImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"图片放大缩小";
table = [[UITableView alloc] initWithFrame:CGRectMake(0,64, ScreenW, ScreenH)];
table.dataSource = self;
table.delegate = self;
[self.view addSubview:table];
[self settingHeaderView];
[self settingHeaderBackgroundView];
}
#pragma mark - 设置头部
-(void)settingHeaderView{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 0, XGHeaderImageH)];
table.tableHeaderView = headerView;
self.headerView = headerView;
}
#pragma mark - 设置backgroundView
-(void)settingHeaderBackgroundView{
UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, ScreenW, XGHeaderImageH)];
headImageView.contentMode = UIViewContentModeScaleAspectFill;
headImageView.image = [UIImage imageNamed:@"13430761,2880,1800.jpg"];
self.headImageView = headImageView;
UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0,0, ScreenW, ScreenH)];
[backGroundView addSubview:headImageView];
table.backgroundView = backGroundView;
}
#pragma mark - 设置滚动放大和缩小
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// 向下滚动offsetY值变为负值,越来越小,向上滚动offsetY值是正值,越来越大
CGFloat offsetY = scrollView.contentOffset.y;
CGRect tempF = self.headImageView.frame;
// 如果offsetY大于0,说明是向上滚动,缩小
if (offsetY > 0) {
tempF.origin.y = -offsetY;
self.headImageView.frame = tempF;
}else{
// 如果offsetY小于0,让headImageView的Y值等于0,headImageView的高度要放大
tempF.size.height = XGHeaderImageH - offsetY;
tempF.origin.y = 0;
self.headImageView.frame = tempF;
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 30;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"hiddenCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
cell.backgroundColor = [UIColor whiteColor];
}
cell.textLabel.text = [NSString stringWithFormat:@"第 %@ 行数据",@(indexPath.row).description];
return cell;
}