分享一个扇形进度条,核心思想是用贝塞尔曲线绘制扇形形成进度条子单位,然后通过修改layer的fillcolor实现进度条前进的效果。
#import "ZKJProgressBar.h"
@interface ZKJProgressBar ()
@property (nonatomic, assign) CGFloat angle;
@property (nonatomic, assign) NSInteger lightBarCounts;
@property (nonatomic, assign) CGFloat lightBarAngle;
@property (nonatomic, assign) CGFloat insideDiameter;
@property (nonatomic, assign) CGFloat outsideDiameter;
@property (nonatomic, strong) NSMutableArray *layerArray;
@property (nonatomic, strong) UIColor *normalColor;
@property (nonatomic, strong) UIColor *highlightColor;
@end
@implementation ZKJProgressBar
- (instancetype)initWithFrame:(CGRect)frame {
if(self= [superinitWithFrame:frame]) {
[selfinitUI];
}
return self;
}
- (void)initUI {
self.backgroundColor = [UIColor yellowColor];
self.layerArray = [[NSMutableArray alloc] initWithCapacity:0];
self.angle=M_PI_4;
self.lightBarCounts = 40;
self.lightBarAngle= (M_PI*2/360) *2;
self.insideDiameter = 80;
self.outsideDiameter = 100;
self.normalColor= [UIColorcyanColor];
self.highlightColor = [UIColor redColor];
if (self.lightBarAngle * self.lightBarCounts > (M_PI * 2 - self.angle)) {
return;
}
CGFloatstartAngle = (M_PI-self.angle) /2+self.angle;
CGFloatendAngle = startAngle +self.lightBarAngle;
CGFloat angleGap = ((M_PI * 2 - self.angle) - self.lightBarCounts * self.lightBarAngle) / (self.lightBarCounts - 1);
for(inti =0; i <self.lightBarCounts; i++) {
UIBezierPath *path = [UIBezierPath bezierPath];
[pathaddArcWithCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2) radius:self.outsideDiameter startAngle:startAngle endAngle:endAngle clockwise:YES];
[pathaddLineToPoint:CGPointMake(self.frame.size.width/2,self.frame.size.height/2)];
[pathclosePath];
CAShapeLayer*layer = [[CAShapeLayeralloc]init];
layer.path= path.CGPath;
layer.strokeColor=self.normalColor.CGColor;
layer.fillColor=self.normalColor.CGColor;
[self.layeraddSublayer:layer];
[self.layerArrayaddObject:layer];
startAngle = startAngle + angleGap +self.lightBarAngle;
endAngle = startAngle +self.lightBarAngle;
}
UIBezierPath *path2 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(self.frame.size.width / 2 - self.insideDiameter, self.frame.size.height / 2 - self.insideDiameter, self.insideDiameter * 2, self.insideDiameter * 2) cornerRadius:self.insideDiameter];
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
layer.path= path2.CGPath;
layer.strokeColor = self.backgroundColor.CGColor;
layer.fillColor = self.backgroundColor.CGColor;
[self.layeraddSublayer:layer];
}
- (void)zkj_setProgress:(NSInteger)progress {
NSIntegerindex = progress /100.0*self.layerArray.count;
for(inti =0; i <self.layerArray.count; i++) {
CAShapeLayer*layer =self.layerArray[i];
if(i < index) {
layer.strokeColor=self.highlightColor.CGColor;
layer.fillColor=self.highlightColor.CGColor;
}else{
layer.strokeColor=self.normalColor.CGColor;
layer.fillColor=self.normalColor.CGColor;
}
}
}
网友评论