@property (nonatomic, assign) double aphle;
@property (nonatomic, strong) QMUINavigationBarScrollingAnimator *navigationAnimator;
首先设置页面样式从y=0布局
if([self respondsToSelector:@selector(edgesForExtendedLayout)]){
self.edgesForExtendedLayout=UIRectEdgeAll;
}
实例化QMUINavigationBarScrollingAnimator
- (void)initNavigationAnimator {
self.navigationAnimator = [[QMUINavigationBarScrollingAnimator alloc] init];
self.navigationAnimator.scrollView = self.pagerView.mainTableView;/// 绑定的 UIScrollView
self.navigationAnimator.offsetYToStartAnimation = 30;// contentOffset.y 到达哪个值即开始动画,默认为 0
self.navigationAnimator.distanceToStopAnimation = NavigationContentTop;/// 控制从 offsetYToStartAnimation 开始,要滚动多长的距离就打到动画结束的位置,默认为 44
}
#pragma mark - <QMUICustomNavigationBarTransitionDelegate>
- (NSString *)customNavigationBarTransitionKey {
return self.navigationAnimator.progress >= 1 ? nil : NSStringFromClass([self class]);
}
- (BOOL)shouldCustomizeNavigationBarTransitionIfHideable {
return YES;
}
设置初始导航栏按钮
- (void)setupNavigationItems {
[super setupNavigationItems];
self.navigationItem.leftBarButtonItem = [UIBarButtonItem qmui_itemWithImage:[UIImage imageNamed:@"ic_white_back"] target:self action:@selector(clickCloseButtonAction)];
self.navigationItem.rightBarButtonItem = [UIBarButtonItem qmui_itemWithImage:[UIImage imageNamed:@"ic_share"] target:self action:@selector(clickShareButtonAction)];
}
通过scrollview滚动代理设置导航栏的样式
- (void)mainTableViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y > 175) {
self.title = @"平台详情";
self.navigationItem.leftBarButtonItem = [UIBarButtonItem qmui_itemWithImage:[UIImage imageNamed:@"icons_black_back"] target:self action:@selector(clickCloseButtonAction)];
self.navigationItem.rightBarButtonItem = [UIBarButtonItem qmui_itemWithImage:[UIImage imageNamed:@"ic_share_black"] target:self action:@selector(clickShareButtonAction)];
}else{
self.title = @"";
self.navigationItem.leftBarButtonItem = [UIBarButtonItem qmui_itemWithImage:[UIImage imageNamed:@"ic_white_back"] target:self action:@selector(clickCloseButtonAction)];
self.navigationItem.rightBarButtonItem = [UIBarButtonItem qmui_itemWithImage:[UIImage imageNamed:@"ic_share"] target:self action:@selector(clickShareButtonAction)];
}
self.aphle = scrollView.contentOffset.y/175;
UIImage *image = [[UIImage qmui_imageWithColor:UIColor.whiteColor] qmui_imageWithAlpha:self.aphle];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
- (void)initNavigationAnimator {
self.navigationAnimator = [[QMUINavigationBarScrollingAnimator alloc] init];
self.navigationAnimator.scrollView = self.scrollView;
self.navigationAnimator.offsetYToStartAnimation = 30;
self.navigationAnimator.distanceToStopAnimation = NavigationContentTop;
@weakify(self);
self.navigationAnimator.backgroundImageBlock = ^UIImage * _Nonnull(QMUINavigationBarScrollingAnimator * _Nonnull animator, float progress) {
@strongify(self);
UIImage *image = [UIImage qmui_imageWithColor:UIColor.whiteColor];//HexColor(@"#5CEAD2")
[self changeNavigationItemStyle:progress];
return [image qmui_imageWithAlpha:progress];
};
}
- (void)changeNavigationItemStyle:(CGFloat)progress {
if(progress >= 0.8){
self.title = self.serviceName;
self.navigationItem.leftBarButtonItem = [UIBarButtonItem qmui_itemWithImage:[UIImage imageNamed:@"icon_back_black"] target:self action:@selector(clickCloseButtonAction)];
self.navigationItem.rightBarButtonItem = [UIBarButtonItem qmui_itemWithImage:[UIImage imageNamed:@"ic_share_black"] target:self action:@selector(clickShareButtonAction)];
}else{
self.title = @"";
self.navigationItem.leftBarButtonItem = [UIBarButtonItem qmui_itemWithImage:[UIImage imageNamed:@"icon_service_back"] target:self action:@selector(clickCloseButtonAction)];
self.navigationItem.rightBarButtonItem = [UIBarButtonItem qmui_itemWithImage:[UIImage imageNamed:@"ic_share_service"] target:self action:@selector(clickShareButtonAction)];
}
}