图片下拉放大
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController
@end
// ViewController.m
#import "ViewController.h"
const CGFloat HMTopViewH = 350; // 设置图片高度
@interface ViewController ()
@property (nonatomic, weak) UIImageView *topView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1. self.tableView.tableHeaderView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"biaoqingdi"]];
// 设置内边距(cell往下移动一段距离)
self.tableView.contentInset = UIEdgeInsetsMake(HMTopViewH * 0.5, 0, 0, 0);
UIImageView *topView = [[UIImageView alloc] init];
topView.image = [UIImage imageNamed:@"biaoqingdi"];
topView.frame = CGRectMake(0, -HMTopViewH, 320, HMTopViewH);
topView.contentMode = UIViewContentModeScaleAspectFill; // 关键代码
// 在指定的index处插入视图[superView insertSubview:grayView atIndex:0];
[self.tableView insertSubview:topView atIndex:0];
self.topView = topView;
}
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
cell.textLabel.text = [NSString stringWithFormat:@"测试数据--%d", indexPath.row];
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 向下拽了多少距离
CGFloat down = -(HMTopViewH * 0.5) - scrollView.contentOffset.y;
if (down < 0) {
return;
}
CGRect frame = self.topView.frame;
// 5决定图片变大的速度,值越大,速度越快
frame.size.height = HMTopViewH + down * 5; // 关键代码
self.topView.frame = frame;
}
@end