Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions LTNavigationBar/BGColorDemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
Expand All @@ -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
Expand Down