一行代码设置UINavigationBar渐变进度条

self.navigationController.navigationBar.sb_progress = 0.8;

GitHub地址

类似于QQ加载网页时候在UINavigationBar上的进度条

要实现一行代码实现渐变进度条需要以下操作

//1.导入UINavigationBar+Item 
//2.在UINavigationController 中实现下面两个方法
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item
{
    navigationBar.sb_gradientProgress.hidden = YES;
    return YES;
}

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
    navigationBar.sb_gradientProgress.hidden = YES;
    return YES;
}
//3.设置进度
self.navigationController.navigationBar.sb_progress = 0.8;

SBGradientProgress 是一个带有渐变动画的View,主要思路是给gradientlayer设置渐变颜色,做key为“colors”的动画

- (void)startAnimation
{
    //设置初始渐变值
    [self gradientColors];
    
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"colors"];
    anim.values = [self animValues];
    [anim setDuration:1];
    [anim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    anim.repeatCount = MAXFLOAT;
    [self.layer addAnimation:anim forKey:nil];
}


/**
 纯色进度条
 */
- (void)setPureColor:(UIColor *)pureColor
{
    _pureColor = pureColor;
    if (pureColor == nil) {
        
        [self startAnimation];
    } else {
        CAGradientLayer *layer = (id)[self layer];
        layer.colors = @[(id)pureColor.CGColor,(id)pureColor.CGColor];
        [self.layer removeAllAnimations];
    }
}


/**
 乾坤大挪移(最后一个放到前面)
 */
- (NSArray *)shiftColors:(NSArray *)colors {
    
    NSMutableArray *mutable = [colors mutableCopy];
    id last = [mutable lastObject];
    [mutable removeLastObject];
    [mutable insertObject:last atIndex:0];
    
    return [NSArray arrayWithArray:mutable];
}



/**
 动画的values <NSArray *<color>>
 */
- (NSArray *)animValues
{
    CAGradientLayer *layer = (id)[self layer];
    NSMutableArray *values = [NSMutableArray array];
    NSArray *colors = nil;
    for (NSInteger deg = 0; deg <= 360; deg += 5) {
        if (values.count) {
            colors = values[deg / 5 - 1];
        } else {
            colors = layer.colors;
        }
        [values addObject:[self shiftColors:colors]];
    }
    
    return values;
}

<img src="http://upload-images.jianshu.io/upload_images/1899979-42a50711400da6a6.gif?imageMogr2/auto-orient/strip" width = "300" height = "500" alt="好像有点大" align=center />

参考项目,思路类似,但做动画的原理不一样

最后祝你们早日脱离苦海

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,267评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,610评论 7 249
  • 清晨,太阳七点钟 阳光从窗里照进铲屎官的屁股上,床上睡着一只喵,床下睡着盖着被子的铲屎官,铲屎官揉揉眼嘀咕 “妈的...
    姓廖阅读 249评论 0 0
  • 忆香山脚下四男儿,居舍读经求真理。 二十载风华已过,平添多少蹉跎! 筚路蓝缕会有时,破茧成蝶谱写新师范为哪般? 真...
    fengtele阅读 202评论 0 0
  • 胖子都好吃,而本胖有别于一般胖子的是,本胖不仅会吃,而且会做,虽然疏懒成性一般不肯动手,可动起手来确实不一般。 广...
    思想的猪阅读 422评论 0 0