美文网首页牛叉的demo恩美第二个APP项目封装
封装一个UINavgationBar的渐变效果

封装一个UINavgationBar的渐变效果

作者: 雪_晟 | 来源:发表于2017-07-06 11:08 被阅读42次

效果图:


滚动渐变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

相关文章

  • 封装一个UINavgationBar的渐变效果

    效果图: 在有UIScrollview的页面中,做一个NavBar 的渐变过渡,最好还是抽取出来。 使用方法,需要...

  • UINavgationbar 渐变透明效果

    问题的产生 产品个需求,修改UINavgationbar 的透明度,以前图省事就把bar给隐藏了,然后自定义了个假...

  • RN渐变按钮

    react-native渐变效果,渐变背景组件封装 组件文件 使用(自己修改一下导入路径)

  • H5-12.15canvas标签应用(一)

    渐变效果 - canvas支持的渐变效果包括线性渐变或射线渐变,并且支持颜色转折点 1)在画布上创建一个渐变对象 ...

  • 04封装的几个运动js

    one 第一个 应用第一个封装的js来实现的透明度的渐变的案例(多物体的淡入淡出) 效果图 应用第一个封装的js...

  • 面向对象实战

    封装一个轮播组件 代码效果预览 封装一个曝光加载组件 效果代码预览 封装一个 Tab 组件 效果代码

  • 面向对象实战

    题目1: 封装一个轮播组件封装轮播组件(效果)代码题目2: 封装一个曝光加载组件封装曝光加载(效果)代码题目3: ...

  • JS面向对象实践

    题目1: 封装一个轮播组件 效果 代码 题目2: 封装一个曝光加载组件 效果 代码 题目3: 封装一个 Tab 组...

  • 面向对象实战

    题目1:封装一个轮播组件 效果demo 题目2:封装一个曝光加载组件 效果demo 题目3:封装一个 Tab 组件...

  • iOS-UILabel字体加边框和渐变色

    最近做了个需求,需要使用字体带边框和渐变色,显示效果如下: 主要方式,封装一个自定义UILabel代码,需要为字体...

网友评论

    本文标题:封装一个UINavgationBar的渐变效果

    本文链接:https://www.haomeiwen.com/subject/xachhxtx.html