利用CAShapeLayer、CAGradientLayer实现弧形曲线渐变色进度条

积满一定条件获取某种勋章或奖励 需要一个弧形的曲线来填充色展示完成进度 有需要这种效果的可以看看

效果图

曲线进度条渐进色动画.gif

方法

创建了两个CAShapeLayer topLayer和bottomLayer bottomLayer来绘制下面灰色弧形的完整部分(表示容量) topLayer用来显示上层的渐变色弧形进度条(表示完成度)

/** 设置top弧形曲线渐变层 */
- (void)gradient {
    
    self.topLayer = [CAShapeLayer layer];
    self.topLayer.lineWidth = layerWidth;
    self.topLayer.lineCap = kCALineCapRound;
    self.topLayer.fillColor = [UIColor clearColor].CGColor;
    self.topLayer.strokeColor = [UIColor whiteColor].CGColor;
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = CGRectMake(0,0,self.frame.size.width,self .frame.size.height);
    gradient.colors = @[(id)[UIColor colorWithRed:1
                                            green:178 / 255.0
                                             blue:101 / 255.0
                                            alpha:1].CGColor,
                            (id)[UIColor colorWithRed:1
                                                green:69 / 255.0
                                                 blue:69 / 255.0
                                                alpha:1].CGColor];
//    [gradient setLocations:@[@0, @0.5, @1]];
    gradient.startPoint = CGPointMake(0, 0);
    gradient.endPoint = CGPointMake(1.0, 0);
    self.topLayer.path = [self arcProgressPath];
    [gradient setMask:self.topLayer];
    [self.layer addSublayer:gradient];
    
    [self setCurrentProgress:_progress];
}

//底部灰色曲线
- (CAShapeLayer *)bottomLayer {
    if (!_bottomLayer) {
        _bottomLayer = [CAShapeLayer layer];
        _bottomLayer.lineCap = kCALineCapRound;
        _bottomLayer.lineWidth = layerWidth;
        _bottomLayer.fillColor = [UIColor clearColor].CGColor;
        UIColor *color = [UIColor colorWithRed:225.0 / 255.0
                                         green:226.0 / 255.0
                                          blue:230.0/255.0
                                         alpha:1];
        _bottomLayer.strokeColor = (self.bottomColor?self.bottomColor:color).CGColor;
        _bottomLayer.path = [self arcProgressPath];
        
        [self.layer addSublayer:_bottomLayer];
    }
    return _bottomLayer;
}
设置弧形路径
//弧形路径
- (CGPathRef)arcProgressPath{
    
    CGPoint origin = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2  );
    CGFloat radius = self.frame.size.width / 2 - layerWidth ;
    
    UIBezierPath *progressPath = [UIBezierPath bezierPathWithArcCenter:origin
                                                                radius:radius
                                                            startAngle:- M_PI_4 * 5
                                                              endAngle:M_PI_4
                                                             clockwise:YES];
    return progressPath.CGPath;
}
设置渐进动画
- (void)setCurrentProgress:(CGFloat)progress {
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration = 2;
    self.topLayer.strokeEnd = progress;
    [self.topLayer addAnimation:animation forKey:@"animationStrokeEnd"];
    [CATransaction commit];
    _progress = progress;

}

demo 地址

LHAnimationDemo

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

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,014评论 3 119
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,250评论 4 61
  • 小时候高楼还没有普及,我们是一家挨着一家住在一个大杂院里。我常常在大杂院前看到一位头发花白的老翁在弯腰捡垃圾。 其...
    荒羽阅读 247评论 1 1
  • 11月15晚,主题为行善宜高调还是低调的辩论赛于人文楼隆重开赛。 赛前准备教室里,比赛负责人做着万无一失的...
    可可柚芷阅读 684评论 1 2