1.我在实现下拉放大的时候出现一个误区,就是要把imageview加到view上 然后tableHeaderView 设置为view 并用在scrollViewDidScroll方法中是要获取tableHeaderView的frame 并把imageView 的frame 改变
2.设置导航栏渐变通过设置alpha来实现导航栏渐变
代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
// 设置导航栏背景色为透明不起作用
// self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
// 设置导航栏为透明
// [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
// [self.navigationController.navigationBar setShadowImage:[UIImage new]];
// 取行导航栏背景图片 通过设置alpha来改变导航栏的透明度
UIImageView *barImageView = self.navigationController.navigationBar.subviews[0];
barImageView.alpha = 0;
_barImageView = barImageView;
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:tableView];
tableView.tableHeaderView = [self header];
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
tableView.delegate = self;
tableView.dataSource = self;
_tableView = tableView;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// NSLog(@"x:%f,y:%f",scrollView.contentOffset.x, scrollView.contentOffset.y);
CGFloat off_y = scrollView.contentOffset.y;
if (off_y < 0) { // 当下拉改变图片图片frame
CGRect frame = _tableView.tableHeaderView.frame;
frame.size.height = frame.size.height - off_y;
frame.origin.y = off_y;
_imageView.frame = frame;
_tableView.tableHeaderView.clipsToBounds = NO;
}
// 在-64到240渐变
CGFloat min = -64;
CGFloat max = 240;
CGFloat alpha = (off_y - min) / (max - min);
_barImageView.alpha = alpha;
}
- (UIView *)header {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.image =[UIImage imageNamed:@"1.jpg"];
// 超出尺寸范围裁剪掉
imageView.clipsToBounds = YES;
_imageView = imageView;
[view addSubview:imageView];
return view;
}