封装一个UINavgationBar的渐变效果

效果图:


滚动渐变NavBar.gif

在有UIScrollview的页面中,做一个NavBar 的渐变过渡,最好还是抽取出来。

使用方法,需要注意的是,恢复navbar的颜色最好是放在viewDidDisappear中,否则侧滑返回的时候,Navbar的颜色会出现问题。渐变的处理,就是把UIScrollview传给工具类即可:

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [LXGradientNavManager resStoreToDefaultNavigationBar];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self setUp];
    
    [LXGradientNavManager managerWithController:self];
    
    [LXGradientNavManager setOrignColor:[UIColor clearColor]];
    
    [LXGradientNavManager setZeroAlphaOffset:0];
    [LXGradientNavManager setFullAlphaOffset:self.tableView.tableHeaderView.height];
    [LXGradientNavManager setDefaultColor:[UIColor hexStringToColor:@"F9F9F9"]];
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [LXGradientNavManager dealGradientWithScrollView:scrollView];
    
}

工具类如下:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface LXGradientNavManager : NSObject
@property(nonatomic,strong)UIColor *defaultColor; //默认的Nav的颜色

@property(nonatomic,strong)UIColor *orignColor; //最开始的颜色

@property(nonatomic,assign)float zeroAlphaOffset;

@property (nonatomic, assign) float fullAlphaOffset;

+(void)setOrignColor:(UIColor *)color;

+ (void)setDefaultColor:(UIColor *)color;

+ (void)setZeroAlphaOffset:(float)offset;

+ (void)setFullAlphaOffset:(float)offset;


+(void)managerWithController:(UIViewController *)viewController;

+(void)dealGradientWithScrollView:(UIScrollView *)scrollView;

+(void)reStoreToSystemNavigationBar; //change the navigationBar to system style

+(void)resStoreToDefaultNavigationBar; //恢复到默认的navbar

@end
#import "LXGradientNavManager.h"

@interface LXGradientNavManager()
@property (nonatomic, strong) UINavigationBar *selfNavigationBar;
@property (nonatomic, strong) UINavigationController *selfNavigationController;
@end
@implementation LXGradientNavManager
+(LXGradientNavManager *)sharedManager{
    
    static LXGradientNavManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[LXGradientNavManager alloc]init];
    });
    return manager;
}

+(void)managerWithController:(UIViewController *)viewController{
    
    if (viewController.navigationController) {
        UINavigationBar *navigationBar = viewController.navigationController.navigationBar;
        [self sharedManager].selfNavigationBar = navigationBar;
        [self sharedManager].selfNavigationController = viewController.navigationController;
        [navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        [navigationBar setShadowImage:[UIImage new]];
    }
}

+(void)dealGradientWithScrollView:(UIScrollView *)scrollView{
    CGFloat y = scrollView.contentOffset.y;
    
    UIColor *defaultColor = [self sharedManager].defaultColor;

    if (y > [self sharedManager].fullAlphaOffset) {
        UIImage *image =[self imageWithColor:defaultColor];

        [[self sharedManager].selfNavigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }else{
        CGFloat alpha = y /[self sharedManager].fullAlphaOffset;
        UIImage *image =[self imageWithColor: [defaultColor colorWithAlphaComponent:alpha]];
        [[self sharedManager].selfNavigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

    }

}

+ (void)reStoreToSystemNavigationBar {
    [[self sharedManager].selfNavigationController setValue:[UINavigationBar new] forKey:@"navigationBar"];
}
+ (void)resStoreToDefaultNavigationBar{
    UIImage *image =[self imageWithColor:[self sharedManager].defaultColor];

    [[self sharedManager].selfNavigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
+(void)setZeroAlphaOffset:(float)offset{
    [self sharedManager].zeroAlphaOffset = offset;
    
}
+(void)setFullAlphaOffset:(float)offset{
    [self sharedManager].fullAlphaOffset = offset;
}
+(void)setDefaultColor:(UIColor *)color{
    [self sharedManager].defaultColor = color;
    
   
    
}
+(void)setOrignColor:(UIColor *)color{
    [self sharedManager].orignColor = color;
    
    [[self sharedManager].selfNavigationBar setBackgroundImage:[self imageWithColor:color] forBarMetrics:UIBarMetricsDefault];
    

    
}
+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [color setFill];
    CGContextFillRect(context, rect);
    UIImage *imgae = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imgae;
}

关于 UIScrollview的起始偏移量,最好还是处理如下。

 self.view.backgroundColor =[UIColor whiteColor];
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;

会保证起始点不会偏移。

demo地址:滚动渐变NavBar

推荐阅读更多精彩内容