static CGFloat ParallaxHeaderHeight = 235
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic,strong) UITableView *tableView
@property (strong, nonatomic) UIImageView *parallaxHeaderView
@property (strong, nonatomic) MASConstraint *parallaxHeaderHeightConstraint
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]
[self.navigationController setNavigationBarHidden:YES animated:NO]
[self.view addSubview:self.tableView]
[self initParallaxHeaderView]
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]
_tableView.backgroundColor = [UIColor clearColor]
_tableView.delegate = self
_tableView.dataSource = self
_tableView.estimatedRowHeight = 0
_tableView.sectionHeaderHeight = 0.0
_tableView.sectionFooterHeight = 0.0
_tableView.estimatedSectionHeaderHeight = 0.0
_tableView.estimatedSectionFooterHeight = 0.0
_tableView.contentInset = UIEdgeInsetsMake(ParallaxHeaderHeight, 0, 0, 0)
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])]
}
return _tableView
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath]
cell.textLabel.text = [NSString stringWithFormat:@"第%ld条测试数据",indexPath.row]
return cell
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y < -ParallaxHeaderHeight) {
_parallaxHeaderHeightConstraint.mas_equalTo(-scrollView.contentOffset.y)
}
}
- (void)initParallaxHeaderView {
_parallaxHeaderView = [[UIImageView alloc] init]
[self.view insertSubview:self.parallaxHeaderView belowSubview:self.tableView]
_parallaxHeaderView.contentMode = UIViewContentModeScaleAspectFill
_parallaxHeaderView.image = [UIImage imageNamed:@"zrx4.jpg"]
[_parallaxHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.view);
make.top.mas_equalTo(self.mas_topLayoutGuideBottom);
_parallaxHeaderHeightConstraint = make.height.mas_equalTo(ParallaxHeaderHeight);
}]
}
@end
