iOS 環形進度條

- (void)drawRect:(CGRect)rect{

CGContextRef context = UIGraphicsGetCurrentContext();

CGRect aRect= CGRectMake(20,20, 160, 160);

CGContextSetRGBStrokeColor(context, 236/255.0, 236/255.0, 236/255.0, 1.0);

CGContextSetLineWidth(context, 30.0);

CGColorRef shadowColor = [UIColor blackColor].CGColor;

//控制圖形陰影顏色

CGContextSetShadowWithColor(context, CGSizeMake(1, 0), 4, shadowColor);

CGContextAddEllipseInRect(context, aRect); //圓環

CGContextDrawPath(context, kCGPathStroke);

}

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height)];

label.textAlignment = NSTextAlignmentCenter;

label.numberOfLines = 3;

NSString * contentStr= @" 75%\n7500\n10000";

NSRange rang = [contentStr rangeOfString:@"%"];

NSMutableAttributedString *textStr = [[NSMutableAttributedString alloc] initWithString:contentStr];

UIColor * color = [[UIColor alloc]initWithRed:228/255.0 green:190/255.0 blue:52/255.0 alpha:1];

[textStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0,rang.location +1)];

[textStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(0,rang.location +1)];

[textStr addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(rang.location + 1,contentStr.length - rang.location -1 )];

label.attributedText = textStr;

//? ? ? ? label.text = @"75%\n7500\n10000";

label.backgroundColor = [UIColor clearColor];

[self addSubview:label];

_progressLayer = [CAShapeLayer new];

[self.layer addSublayer:_progressLayer];

_progressLayer.fillColor = nil;

_progressLayer.lineCap = kCALineCapRound;

_progressLayer.frame = self.bounds;

//默認5

self.progressWidth = 5;

}

return self;

}

- (void)setProgress

{

_progressPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2,self.frame.size.height/2) radius:84 startAngle:- M_PI_2 endAngle:(M_PI * 2) * _progress - M_PI_2 clockwise:YES];

_progressLayer.path = _progressPath.CGPath;

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

pathAnimation.duration = 1.0;

pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

pathAnimation.fromValue = @0.0f;

pathAnimation.toValue = @1.0f;

[_progressLayer addAnimation:pathAnimation forKey:nil];

_progressLayer.strokeEnd = 1.0;

}

- (void)setProgressWidth:(float)progressWidth

{

_progressWidth = progressWidth;

_progressLayer.lineWidth = _progressWidth;

[self setProgress];

}

- (void)setProgressColor:(UIColor *)progressColor

{

_progressLayer.strokeColor = progressColor.CGColor;

}

- (void)setProgress:(float)progress

{

_progress = progress;

[self setProgress];

}

//漸變動畫

-(CABasicAnimation*)fadeAnimation

{

CABasicAnimation* fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];

fadeAnimation.fromValue = [NSNumber numberWithFloat:0.0];

fadeAnimation.toValue = [NSNumber numberWithFloat:1.0];

fadeAnimation.duration = 2.0;

return fadeAnimation;

}

效果圖:

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容