diff --git a/LTNavigationBar/BGColorDemoViewController.m b/LTNavigationBar/BGColorDemoViewController.m index 87cca77..cef1580 100644 --- a/LTNavigationBar/BGColorDemoViewController.m +++ b/LTNavigationBar/BGColorDemoViewController.m @@ -9,7 +9,7 @@ #import "BGColorDemoViewController.h" #import "UINavigationBar+Awesome.h" -#define NAVBAR_CHANGE_POINT 50 +#define NAVBAR_CHANGE_POINT self.tableView.tableHeaderView.frame.size.height - self.tableView.contentInset.top * 2 @interface BGColorDemoViewController () @property (weak, nonatomic) IBOutlet UITableView *tableView; @@ -23,18 +23,30 @@ - (void)viewDidLoad { self.tableView.dataSource = self; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]]; + // 是否缩放变形 + [self.tableView.tableHeaderView.subviews lastObject].contentMode = UIViewContentModeScaleAspectFill; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { - UIColor * color = [UIColor colorWithRed:0/255.0 green:175/255.0 blue:240/255.0 alpha:1]; - CGFloat offsetY = scrollView.contentOffset.y; - if (offsetY > NAVBAR_CHANGE_POINT) { - CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - offsetY) / 64)); - [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]]; - } else { - [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]]; - } + UIColor * color = [UIColor colorWithRed:0/255.0 green:175/255.0 blue:240/255.0 alpha:1]; + CGFloat offsetY = scrollView.contentOffset.y; + UIView *headerImage = [self.tableView.tableHeaderView.subviews lastObject]; + if (offsetY > NAVBAR_CHANGE_POINT) { // 渐变色 + CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - offsetY) / 64)); + [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]]; + } else { // 纯色 + [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]]; + if (offsetY < - self.tableView.contentInset.top) { // 到顶了还下拉 + // 缩放方式二: + // CGFloat scale = 1 - (offsetY + self.tableView.contentInset.top)/500; + // headerImage.transform = CGAffineTransformMakeScale(scale, scale); + CGRect frame = headerImage.frame; + frame.origin.y = offsetY; + frame.size.height = -offsetY + self.tableView.tableHeaderView.frame.size.height; + headerImage.frame = frame; + } + } } - (void)viewWillAppear:(BOOL)animated