ZCCCircleProgressView.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // ZCCCircleProgressView.m
  3. // MOSOBOStudent
  4. //
  5. // Created by mac on 2017/10/23.
  6. // Copyright © 2017年 zcc. All rights reserved.
  7. //
  8. #import "ZCCCircleProgressView.h"
  9. @interface ZCCCircleProgressView()
  10. @property (nonatomic, strong)CAGradientLayer *gradientLayer;
  11. //进度圆环
  12. @property (nonatomic, strong) CAShapeLayer *shapeLayer;
  13. @property (nonatomic, strong) CAShapeLayer *shapeLayer2;
  14. @property (nonatomic, strong)NSTimer *timer;
  15. @end
  16. @implementation ZCCCircleProgressView
  17. - (instancetype)initWithFrame:(CGRect)frame{
  18. if(self = [super initWithFrame:frame]){
  19. self.backgroundColor = [UIColor whiteColor];
  20. self.lineWidth = frame.size.width/8;
  21. [self addCircleWithColor:[UIColor purpleColor]];
  22. }
  23. return self;
  24. }
  25. - (void)addCircleWithColor:(UIColor *)color{
  26. CGFloat width = self.bounds.size.width;
  27. CGFloat height = self.bounds.size.height;
  28. UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(width / 2, height / 2) radius:(width - self.lineWidth)/2 startAngle:M_PI / 2+0.01 endAngle:M_PI / 2 clockwise:YES];
  29. CAShapeLayer *bgLayer = [CAShapeLayer layer];
  30. bgLayer.frame = self.bounds;
  31. bgLayer.fillColor = [UIColor clearColor].CGColor;
  32. bgLayer.lineWidth = self.lineWidth;
  33. bgLayer.strokeColor = SDColor(212, 212, 212, 1.0).CGColor;
  34. bgLayer.strokeStart = 0;
  35. bgLayer.strokeEnd = 1;
  36. bgLayer.lineCap = kCALineCapRound;
  37. bgLayer.path = circlePath.CGPath;
  38. [self.layer addSublayer:bgLayer];
  39. _shapeLayer = [CAShapeLayer layer];
  40. _shapeLayer.frame = self.bounds;
  41. _shapeLayer.fillColor = [UIColor clearColor].CGColor;
  42. _shapeLayer.lineWidth = self.lineWidth;
  43. _shapeLayer.lineCap = kCALineCapRound;
  44. _shapeLayer.strokeColor = [UIColor blueColor].CGColor;
  45. _shapeLayer.strokeStart = 0;
  46. _shapeLayer.strokeEnd = 0;
  47. _shapeLayer.path = circlePath.CGPath;
  48. [self.layer addSublayer:_shapeLayer];
  49. if(self.hasSubProgress) {
  50. UIBezierPath *circlePath2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(width / 2, height / 2) radius:(width - 4 * self.lineWidth)/2 startAngle:M_PI / 2+0.01 endAngle:M_PI / 2 clockwise:YES];
  51. CAShapeLayer *bgLayer2 = [CAShapeLayer layer];
  52. bgLayer2.frame = self.bounds;
  53. bgLayer2.fillColor = [UIColor clearColor].CGColor;
  54. bgLayer2.lineWidth = self.lineWidth;
  55. bgLayer2.strokeColor = SDColor(212, 212, 212, 1.0).CGColor;
  56. bgLayer2.strokeStart = 0;
  57. bgLayer2.strokeEnd = 1;
  58. bgLayer2.lineCap = kCALineCapRound;
  59. bgLayer2.path = circlePath2.CGPath;
  60. [self.layer addSublayer:bgLayer2];
  61. _shapeLayer2 = [CAShapeLayer layer];
  62. _shapeLayer2.frame = self.bounds;
  63. _shapeLayer2.fillColor = [UIColor clearColor].CGColor;
  64. _shapeLayer2.lineWidth = self.lineWidth;
  65. _shapeLayer2.lineCap = kCALineCapRound;
  66. _shapeLayer2.strokeColor = [UIColor blueColor].CGColor;
  67. _shapeLayer2.strokeStart = 0;
  68. _shapeLayer2.strokeEnd = 0;
  69. _shapeLayer2.path = circlePath2.CGPath;
  70. [self.layer addSublayer:_shapeLayer2];
  71. }
  72. self.gradientLayer = [CAGradientLayer layer];
  73. CAGradientLayer *leftGradientLayer = [CAGradientLayer layer];
  74. leftGradientLayer.frame = CGRectMake(0, 0, width / 2, height);
  75. [leftGradientLayer setColors:[NSArray arrayWithObjects:(id)SDColor(255, 255, 0, 1).CGColor, (id)SDColor(255, 255.0/2, 0, 1).CGColor, nil]];
  76. [leftGradientLayer setLocations:@[@0,@0.9]];
  77. [leftGradientLayer setStartPoint:CGPointMake(0, 1)];
  78. [leftGradientLayer setEndPoint:CGPointMake(1, 0)];
  79. [_gradientLayer addSublayer:leftGradientLayer];
  80. CAGradientLayer *rightGradientLayer = [CAGradientLayer layer];
  81. rightGradientLayer.frame = CGRectMake(width / 2, 0, width / 2, height);
  82. [rightGradientLayer setColors:[NSArray arrayWithObjects:(id)SDColor(255, 255.0 / 2, 0, 1.0).CGColor, (id)SDColor(255, 0, 0, 1.0).CGColor, nil]];
  83. [rightGradientLayer setLocations:@[@0.1, @1]];
  84. [rightGradientLayer setStartPoint:CGPointMake(0.5, 0)];
  85. [rightGradientLayer setEndPoint:CGPointMake(0.5, 1)];
  86. [_gradientLayer addSublayer:rightGradientLayer];
  87. [self.gradientLayer setMask:_shapeLayer];
  88. self.gradientLayer.frame = self.bounds;
  89. [self.layer addSublayer:self.gradientLayer];
  90. }
  91. - (void)animateToProgress:(CGFloat)progress subProgress:(CGFloat)subProgress {
  92. if(_shapeLayer.strokeEnd != 0){
  93. [self animateToZero];
  94. }
  95. __weak typeof(self)weakSelf = self;
  96. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_shapeLayer.strokeEnd * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  97. [weakSelf deleteTimer];
  98. NSDictionary *userInfo = @{@"progressStr":@(progress),@"subProgress":@(subProgress)};
  99. weakSelf.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:weakSelf selector:@selector(animate:) userInfo:userInfo repeats:YES];
  100. });
  101. }
  102. - (void)animate:(NSTimer *)time {
  103. CGFloat progress = [[time.userInfo objectForKey:@"progressStr"] floatValue];
  104. CGFloat subProgress = [[time.userInfo objectForKey:@"subProgress"] floatValue];
  105. if(_shapeLayer.strokeEnd <= progress || (self.hasSubProgress && _shapeLayer2.strokeEnd <= subProgress)) {
  106. if(_shapeLayer.strokeEnd <= progress) {
  107. _shapeLayer.strokeEnd += 0.01;
  108. }
  109. if(self.hasSubProgress && _shapeLayer2.strokeEnd <= subProgress) {
  110. _shapeLayer2.strokeEnd += 0.01;
  111. }
  112. } else {
  113. [self deleteTimer];
  114. }
  115. }
  116. - (void)animateToZero{
  117. [self deleteTimer];
  118. self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(animateReset) userInfo:nil repeats:YES];
  119. }
  120. - (void)animateReset{
  121. if(_shapeLayer.strokeEnd > 0 || (self.hasSubProgress && _shapeLayer2.strokeEnd > 0)) {
  122. if(_shapeLayer.strokeEnd > 0) {
  123. _shapeLayer.strokeEnd -= 0.01;
  124. }
  125. if(self.hasSubProgress && _shapeLayer2.strokeEnd > 0) {
  126. _shapeLayer2.strokeEnd -= 0.01;
  127. }
  128. }else{
  129. [self deleteTimer];
  130. }
  131. }
  132. - (void)setProgress:(CGFloat)progress subProgress:(CGFloat)subProgress {
  133. _shapeLayer.strokeEnd = progress;
  134. if(self.hasSubProgress) {
  135. _shapeLayer2.strokeEnd = subProgress;
  136. }
  137. }
  138. - (void)reset {
  139. _shapeLayer.strokeEnd = 0;
  140. if(self.hasSubProgress) {
  141. _shapeLayer2.strokeEnd = 0;
  142. }
  143. }
  144. - (void)deleteTimer{
  145. [self.timer invalidate];
  146. self.timer = nil;
  147. }
  148. @end