一个方法搞定表头拉伸效果
演示效果
自定义view,imageView附在view上,view做表头
viewdidLoad
static float headerHeght; //图片高度
- (void)viewDidLoad {
[super viewDidLoad];
headerHeght = 100;
self.automaticallyAdjustsScrollViewInsets = NO;
_tab.delegate = self;
_tab.dataSource = self;
self.navigationItem.title = @"表头测试";
// 表头自定义view
UIView *headerVi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, headerHeght)];
// 填充表头imageView
_headerIma = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, headerHeght)];
_headerIma.image = [UIImage imageNamed:@"星空.jpeg"];
[headerVi addSubview:_headerIma];
_tab.tableHeaderView = headerVi;
}
滚动拉伸方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat YOffset = scrollView.contentOffset.y;
NSLog(@"%f",scrollView.contentOffset.y);
CGFloat width = self.view.frame.size.width;
if (YOffset < 0) {
CGFloat allOffset = headerHeght + ABS(YOffset);
CGFloat scale = allOffset / headerHeght ;
self.headerIma.frame = CGRectMake(- (width * scale - width) / 2, YOffset, width * scale, allOffset);
}
}
网友评论