tableViewheader下拉放大.zip
说明:当tableView下拉的时候,上面的图根据偏移量改变尺寸,当偏移量为负值的时候(下拉),可以改变尺寸,当偏移量为正值的时候(上拉),不改变尺寸。
#import "ViewController.h"&#64;interface ViewController ()<UITableViewDelegate,UITableViewDataSource>&#64;property (weak, nonatomic) IBOutlet UITableView *tableView;
&#64;property (nonatomic,retain) UIView *headerView; &#64;end&#64;implementation ViewController- (void)viewDidLoad {[super viewDidLoad];[self.view addSubview:self.headerView];self.tableView.delegate &#61; self;self.tableView.dataSource &#61; self;}#pragma mark 设置每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 60;
}#pragma mark 设置section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;
}#pragma mark 设置每个section的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return 20;
}#pragma mark cellForRow方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *cellIdentifier &#61; &#64;"cell";UITableViewCell *cell &#61; [tableView dequeueReusableCellWithIdentifier:cellIdentifier];if (cell &#61;&#61; nil) {cell &#61; [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];}cell.selectionStyle &#61; UITableViewCellSelectionStyleDefault;cell.backgroundColor &#61; [UIColor clearColor];return cell;
}#pragma mark 添加点击cell的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {[tableView deselectRowAtIndexPath:indexPath animated:YES];
}- (void)scrollViewDidScroll:(UIScrollView *)scrollView {if (scrollView.contentOffset.y<0) {self.headerView.frame &#61; CGRectMake(scrollView.contentOffset.y/200*WIDTH/2, 0, (200-scrollView.contentOffset.y)/200*WIDTH, 200-scrollView.contentOffset.y);}NSLog(&#64;"正在滑动");
}- (UIView *)headerView {if (!_headerView) {_headerView &#61; [[UIView alloc] init];_headerView.frame &#61; CGRectMake(0, 0, WIDTH, 200);UIImageView *imageView &#61; [[UIImageView alloc] init];imageView.image &#61; [UIImage imageNamed:&#64;"图片"];[_headerView addSubview:imageView];imageView.translatesAutoresizingMaskIntoConstraints &#61; NO;NSArray *hConstranits &#61; [NSLayoutConstraint constraintsWithVisualFormat:&#64;"H:|-0-[imageView]-0-|" options:0 metrics:nil views:&#64;{&#64;"imageView":imageView}];NSArray *vConstranits &#61; [NSLayoutConstraint constraintsWithVisualFormat:&#64;"V:|-0-[imageView]-0-|" options:0 metrics:nil views:&#64;{&#64;"imageView":imageView}];[_headerView addConstraints:hConstranits];[_headerView addConstraints:vConstranits];}return _headerView;
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];
}&#64;end