帶漸變顏色的環(huán)形進(jìn)度條

創(chuàng)建一個(gè)circleView,以后要用到環(huán)形進(jìn)度條實(shí)例化之就可以

#import"cycleView.h"

@implementationcycleView

- (void)drawRect:(CGRect)rect

{

//使用CAShapeLayer繪制出漸變層,因?yàn)樗荒苤付▋蓚€(gè)點(diǎn)之間進(jìn)行漸變,所以這里需要兩個(gè)CAShapeLayer,左右各一個(gè)

//生成漸變色

CALayer*gradientLayer = [CALayerlayer];

//左側(cè)漸變色

CAGradientLayer*leftLayer = [CAGradientLayerlayer];

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

//分段設(shè)置漸變色

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

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

[gradientLayeraddSublayer:leftLayer];

//右側(cè)漸變色

CAGradientLayer*rightLayer = [CAGradientLayerlayer];

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

//分段設(shè)置漸變色

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);

//設(shè)置半徑

CGFloat radius = 90;

//圓起始點(diǎn)位置

CGFloat startA = - M_PI_2;

//圓終點(diǎn)位置

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

//

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

//設(shè)置線條寬度

CGContextSetLineWidth(ctx, 10);

//設(shè)置描邊顏色

[[UIColor blueColor] setStroke];

//把路徑添加到上下文

CGContextAddPath(ctx, path.CGPath);

//渲染

CGContextStrokePath(ctx);

*/

}

//drawRect方法只在視圖剛出現(xiàn)執(zhí)行一次,所以需要使用[self setNeedDisplay];

- (void)drawProgress:(CGFloat)progress

{

_progress= progress;

[selfsetNeedsDisplay];

}

效果圖:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容