美文网首页ios专题
带渐变颜色的环形进度条

带渐变颜色的环形进度条

作者: Johnny_Chang | 来源:发表于2016-03-13 00:30 被阅读506次

    创建一个circleView,以后要用到环形进度条实例化之就可以

    #import"cycleView.h"

    @implementationcycleView

    - (void)drawRect:(CGRect)rect

    {

    //使用CAShapeLayer绘制出渐变层,因为它只能指定两个点之间进行渐变,所以这里需要两个CAShapeLayer,左右各一个

    //生成渐变色

    CALayer*gradientLayer = [CALayerlayer];

    //左侧渐变色

    CAGradientLayer*leftLayer = [CAGradientLayerlayer];

    leftLayer.frame=CGRectMake(0,0,self.bounds.size.width/2,self.bounds.size.height);

    //分段设置渐变色

    leftLayer.locations=@[@0.3,@0.9,@1];

    leftLayer.colors=@[(id)[UIColoryellowColor].CGColor,(id)[UIColorgreenColor].CGColor];

    [gradientLayeraddSublayer:leftLayer];

    //右侧渐变色

    CAGradientLayer*rightLayer = [CAGradientLayerlayer];

    rightLayer.frame=CGRectMake(self.bounds.size.width/2,0,self.bounds.size.width/2,self.bounds.size.height);

    //分段设置渐变色

    rightLayer.locations=@[@0.3,@0.9,@1];

    rightLayer.colors=@[(id)[UIColoryellowColor].CGColor,(id)[UIColorredColor].CGColor];

    [gradientLayeraddSublayer:rightLayer];

    CGPointcenter =CGPointMake(100,100);

    CGFloatradius =90;

    CGFloatstartA = -M_PI_2;

    CGFloatendA = -M_PI_2+M_PI*2*_progress;

    CAShapeLayer*progressLayer = [CAShapeLayerlayer];

    progressLayer.frame=self.bounds;

    progressLayer.fillColor= [UIColorclearColor].CGColor;

    progressLayer.strokeColor= [UIColorredColor].CGColor;

    progressLayer.opacity=1;

    progressLayer.lineCap=kCALineCapRound;

    progressLayer.lineWidth=10;

    UIBezierPath*path = [UIBezierPathbezierPathWithArcCenter:centerradius:radiusstartAngle:startAendAngle:endAclockwise:YES];

    progressLayer.path= path.CGPath;

    [self.layeraddSublayer:progressLayer];

    [gradientLayersetMask:progressLayer];

    [self.layeraddSublayer:gradientLayer];

    /*

    //获取上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //获取圆心的位置

    CGPoint center = CGPointMake(100, 100);

    //设置半径

    CGFloat radius = 90;

    //圆起始点位置

    CGFloat startA = - M_PI_2;

    //圆终点位置

    CGFloat endA = - M_PI_2 + M_PI * 2 *_progress;

    //

    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];

    //设置线条宽度

    CGContextSetLineWidth(ctx, 10);

    //设置描边颜色

    [[UIColor blueColor] setStroke];

    //把路径添加到上下文

    CGContextAddPath(ctx, path.CGPath);

    //渲染

    CGContextStrokePath(ctx);

    */

    }

    //drawRect方法只在视图刚出现执行一次,所以需要使用[self setNeedDisplay];

    - (void)drawProgress:(CGFloat)progress

    {

    _progress= progress;

    [selfsetNeedsDisplay];

    }

    效果图:

    相关文章

      网友评论

        本文标题:带渐变颜色的环形进度条

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