iOS 环形进度条

2016-07-12  本文已影响195人  _桃夭大人_

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

}

效果图:

上一篇 下一篇

猜你喜欢

热点阅读