iOS 渐变色进度条 + 紧急样式

近日我们设计小姐姐重新设计了一款 进度条,然后最近也不是很忙,那就特此记录下这次的过程咯

要的就是图中 进度条的效果
  • 渐变色
  • 紧急样式
  • 动画
一、渐变色 无疑是 用 CAGradientLayer
- (CAGradientLayer *)gradientLayer {
    if (!_gradientLayer) {
        _gradientLayer = [CAGradientLayer layer];
        _gradientLayer.bounds = CGRectMake(0, 0, self.frame.size.width * self.progress, self.frame.size.height);
        _gradientLayer.startPoint = CGPointMake(0, 0.5);
        _gradientLayer.endPoint = CGPointMake(1, 0);
        _gradientLayer.anchorPoint = CGPointMake(0, 0);
        _gradientLayer.colors = self.colorArr;
        _gradientLayer.cornerRadius = self.frame.size.height / 2.;
    }
    return _gradientLayer;
}
/**
 *  进度条渐变颜色数组,颜色个数>=2
    默认是:@[(__bridge id)[UIColor orangeColor].CGColor, (__bridge id)[UIColor yellowColor].CGColor];
 */
@property (nonatomic, strong) NSArray *colorArr;
二、紧急样式 直接用 UIBezierPath
- (void)addStripesEmergency {
    CGRect rect = self.gradientLayer.frame;
    CGFloat xStart = rect.size.height/2.0, height = rect.size.height, width = rect.size.height;
    while (xStart < rect.size.width - rect.size.height/2.0) {
        CAShapeLayer *layer = [[CAShapeLayer alloc] init];
        // 初始化
        UIBezierPath* aPath = [UIBezierPath bezierPath];
        // 起始点
        [aPath moveToPoint:CGPointMake(xStart, 0)];
        // 画线
        [aPath addLineToPoint:CGPointMake(xStart + width * 0.25, height)]; // 第一点
        [aPath addLineToPoint:CGPointMake(xStart + width * 0.75, height)]; // 第二点
        [aPath addLineToPoint:CGPointMake(xStart + width * 0.50, 0)]; // 第三点
        //通过调用closePath方法得到的
        [aPath closePath];
        xStart += 1.5 * width;
        layer.path = aPath.CGPath;
        layer.fillColor = [UIColor colorWithWhite:0 alpha:0.2].CGColor;
        [self.gradientLayer addSublayer:layer];
    }
}
三、动画用 CABasicAnimation
- (CABasicAnimation *)basicAnimation {
    if (!_basicAnimation) {
        _basicAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
        _basicAnimation.duration = 1.0;
        _basicAnimation.fillMode = kCAFillModeForwards;
        _basicAnimation.repeatCount = 1.0;
        _basicAnimation.delegate = self;
        _basicAnimation.removedOnCompletion = NO;
        _basicAnimation.fillMode = kCAFillModeForwards;
        _basicAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    }
    return _basicAnimation;
}
- (void)setAnimation:(BOOL)animation {
    if (animation) {
        self.basicAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 0, self.gradientLayer.frame.size.height)];
        self.basicAnimation.toValue = [NSValue valueWithCGRect:self.gradientLayer.frame];
        [self.gradientLayer addAnimation:self.basicAnimation forKey:@"gradientAnimaiton"];
    }
}

感觉好久没画图了,小尝试下,熟练下

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

推荐阅读更多精彩内容

  • 1 CALayer IOS SDK详解之CALayer(一) http://doc.okbase.net/Hell...
    Kevin_Junbaozi阅读 5,210评论 3 23
  • 最终效果大概是这样滴,动画要求是时长共两秒。第一秒进度条滑动至进度的90%,第二秒滑动剩下的10%,中间数字跟随滑...
    Tony_HYH阅读 12,351评论 13 32
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,250评论 4 61
  • 出夏解花迟,伤秋别有思。 爱莲而羨藕,惜柳不折枝。 风静幽蝉噪,蛙鸣睡鲤痴。 约期无二月,枯梗满清池。
    张成昱阅读 1,399评论 13 52
  • 转:我们办公空间里有个摊煎饼果子的档口。中午我买煎饼结账的时候拿出手机,由于微信改版我找了半天也找不到支付的入口在...
    萝卜牛肉阅读 546评论 0 0