GQScheduleView.h
@interface GQScheduleView : UIView
@property (nonatomic, strong) NSArray *textArr;
@property (nonatomic, strong) UIColor *col;
@property (nonatomic, assign) NSInteger statesNum;
@end
GQScheduleView.m
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
static CGFloat radius = 5;
static CGFloat lineWidth = 2;
static CGFloat leftSep = 30;
static CGFloat circleLabSep = 10;
@interface GQScheduleView()
@property (nonatomic, strong) NSMutableArray *labelArr;
@end
@implementation GQScheduleView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CAShapeLayer *shapelayer = [self setupShapeLayerWithStrokeColor:self.col];
[self.layer addSublayer:shapelayer];
CGFloat lineW = (rect.size.width - 2 * leftSep - self.textArr.count * (2 * radius)) / (self.textArr.count - 1);
CGFloat totalLength = 0.0;
CGFloat firstLength = 0.0;
CGFloat firstTotalTime= 0.0;
CGFloat lastTotalTime= 0.0;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(leftSep + radius, radius) radius:radius startAngle:-M_PI endAngle:M_PI clockwise:YES];
path.lineWidth = lineWidth;
firstLength += totalLength + (2 * radius * M_PI);
totalLength = totalLength + (2 * radius * M_PI);
// label
[self addLabelWithText:self.textArr[0] center:CGPointMake(leftSep + radius, radius * 2 + circleLabSep)];
UIBezierPath *path2 = [UIBezierPath bezierPath];
for (NSInteger i = 0; i < self.textArr.count - 1; ++i) { // 3
if (i + 2 <= self.statesNum) { // 0+2 > 2
[path moveToPoint:CGPointMake(leftSep + radius * 2 + (lineW + radius * 2) * i + lineWidth, radius)];
[path addLineToPoint:CGPointMake(leftSep + radius * 2 + lineW + (radius * 2 + lineW) * i, radius)];
firstLength += lineW;
totalLength += lineW;
[path addArcWithCenter:CGPointMake(leftSep + radius * 2 + lineW + radius + (lineW + radius * 2) * i, radius) radius:radius startAngle:-M_PI endAngle:M_PI clockwise:YES];
firstLength += (2 * radius * M_PI);
totalLength += (2 * radius * M_PI);
} else {
[path2 moveToPoint:CGPointMake(leftSep + radius * 2 + (lineW + radius * 2) * i + lineWidth, radius)];
[path2 addLineToPoint:CGPointMake(leftSep + radius * 2 + lineW + (radius * 2 + lineW) * i, radius)];
totalLength += lineW;
[path2 addArcWithCenter:CGPointMake(leftSep + radius * 2 + lineW + radius + (lineW + radius * 2) * i, radius) radius:radius startAngle:-M_PI endAngle:M_PI clockwise:YES];
totalLength += (2 * radius * M_PI);
}
[self addLabelWithText:self.textArr[i + 1] center:CGPointMake(leftSep + radius * 2 + lineW + radius + (lineW + radius * 2) * i, radius * 2 + circleLabSep)];
}
CGFloat totalTime = self.textArr.count * 0.5;
CGFloat perTime = totalTime / totalLength;
firstTotalTime += (perTime * firstLength);
lastTotalTime = totalTime - firstTotalTime;
shapelayer.path = path.CGPath;
CABasicAnimation * baseAnimation = [self setupAnimaWithDuration:firstTotalTime];
CABasicAnimation * baseAnimation2 = [self setupAnimaWithDuration:lastTotalTime];
[shapelayer addAnimation:baseAnimation forKey:@"strokeEnd"];
CAShapeLayer *shapelayer2 = [self setupShapeLayerWithStrokeColor:[UIColor lightGrayColor]];
[self.layer addSublayer:shapelayer2];
[UIView animateWithDuration:perTime * (2 * radius * M_PI) delay:0 options:0 animations:^{
UILabel *lab = self.labelArr[0];
lab.alpha = 1;
} completion:nil];
for (NSInteger i = 1; i < self.labelArr.count; ++i) {
[UIView animateWithDuration:perTime * (2 * radius * M_PI + lineW) delay:perTime * (2 * radius * M_PI) + perTime * (2 * radius * M_PI + lineW) * (i-1) options:0 animations:^{
UILabel *lab = self.labelArr[i];
lab.alpha = 1;
} completion:nil];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(firstTotalTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
shapelayer2.path = path2.CGPath;
[shapelayer2 addAnimation:baseAnimation2 forKey:@"strokeEnd"];
});
}
- (CABasicAnimation * )setupAnimaWithDuration:(CGFloat)dur {
CABasicAnimation * baseAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
baseAnimation.fromValue = @0;
baseAnimation.toValue = @1;
baseAnimation.duration = dur;
baseAnimation.repeatCount = 1;
baseAnimation.removedOnCompletion = NO;
baseAnimation.fillMode = kCAFillModeForwards;
return baseAnimation;
}
- (CAShapeLayer *)setupShapeLayerWithStrokeColor:(UIColor *)strokeColor {
CAShapeLayer *shapelayer = [CAShapeLayer layer];
shapelayer.lineWidth = lineWidth;
shapelayer.cornerRadius = lineWidth;
shapelayer.lineCap = kCALineJoinRound;
shapelayer.fillColor = [UIColor clearColor].CGColor;
shapelayer.strokeColor = strokeColor.CGColor;
return shapelayer;
}
- (void)addLabelWithText:(NSString *)text center:(CGPoint)center {
UILabel *textLab = [[UILabel alloc] init];
textLab.text = text;
textLab.font = [UIFont systemFontOfSize:14];
[textLab sizeToFit];
textLab.center = center;
[self addSubview:textLab];
textLab.alpha = 0;
[self.labelArr addObject:textLab];
}
- (NSMutableArray *)labelArr {
if (_labelArr == nil) {
_labelArr = [[NSMutableArray alloc] init];
}
return _labelArr;
}
@end