From b4eb194b4b9adc8e3d7291a643fe76dc7f20d83f Mon Sep 17 00:00:00 2001 From: LinBq Date: Tue, 3 May 2016 19:36:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=8B=E6=8B=89=E6=94=BE?= =?UTF-8?q?=E5=A4=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LTNavigationBar/BGColorDemoViewController.m | 30 ++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) 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