MyView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // MyView.m
  3. // 进度
  4. //
  5. // Created by 陈孟迪 on 2018/1/24.
  6. // Copyright © 2018年 陈孟迪. All rights reserved.
  7. //
  8. #import "MyView.h"
  9. #import "UIColor+hexColor.h"
  10. #define space 30
  11. #define topMargin 15
  12. #define radius1 (self.frame.size.width-space*2)/3.5
  13. #define radius2 radius1-10
  14. #define radius3 radius2-radius1/2
  15. #define radius4 10
  16. #define number 101
  17. #define greenColor @"008000"
  18. //弧度转角度
  19. #define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
  20. //角度转弧度
  21. #define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
  22. @interface MyView ()
  23. {
  24. UIView *pointView ;
  25. CGFloat angle;//总弧度
  26. NSMutableArray *scaleLayer;
  27. CAGradientLayer *gradientLayer2;
  28. CAShapeLayer *layer9;//内圆的进度
  29. CAShapeLayer *layer5;//内圆左边的线
  30. CAShapeLayer *layer6;//内圆右边的线
  31. CGFloat perAngle;
  32. CGFloat backAngle;
  33. UILabel *lblText ;
  34. }
  35. //刻度数目
  36. @property (nonatomic,assign)int scaleNumber;
  37. //开始弧度
  38. @property (nonatomic,assign)CGFloat start;
  39. //结束弧度
  40. @property (nonatomic,assign)CGFloat end;
  41. @end
  42. @implementation MyView
  43. - (instancetype)initWithFrame:(CGRect)frame
  44. {
  45. self = [super initWithFrame:frame];
  46. if (self) {
  47. self.backgroundColor = [UIColor whiteColor];
  48. _progressColor = [UIColor hexColor:greenColor];
  49. _scaleNumber = number;
  50. _start = M_PI*5/6+M_PI/12;
  51. _end = M_PI/6-M_PI/12;
  52. angle = M_PI*4/3-M_PI/12*2;//M_PI+M_PI/6+M_PI/6-M_PI/12*2 计算总弧度
  53. perAngle = angle/(_scaleNumber-1);//根据数量计算每个刻度占的弧度
  54. [self createUI];
  55. }
  56. return self;
  57. }
  58. - (void)setProgressColor:(UIColor *)progressColor{
  59. _progressColor = progressColor;
  60. layer9.strokeColor = _progressColor.CGColor;
  61. pointView.backgroundColor = _progressColor;
  62. [self updateLineColor];
  63. }
  64. - (void)createUI{
  65. [self drawCircle];
  66. [self drawScale:[UIColor hexColor:greenColor] AndNumber:0];
  67. // [self drawProgressCircle];
  68. [self drawLine];
  69. [self drawPointer];
  70. [self drawOther];
  71. // [self drawText];
  72. }
  73. - (void)drawOther{
  74. //画圆心
  75. UIBezierPath *path4 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2,radius1+15) radius:radius4 startAngle:0 endAngle:M_PI*2 clockwise:YES];
  76. CAShapeLayer *layer4 = [[CAShapeLayer alloc]init];
  77. layer4. fillColor = nil;
  78. layer4.strokeColor = [UIColor whiteColor].CGColor;
  79. layer4.path = path4.CGPath;
  80. layer4.lineWidth = 1;
  81. [self.layer addSublayer:layer4];
  82. // //下边的圆
  83. // UIBezierPath *path8 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2,radius1+topMargin) radius:radius3 startAngle:M_PI*3/4 endAngle:M_PI/4 clockwise:NO];
  84. // CAShapeLayer *layer8 = [[CAShapeLayer alloc]init];
  85. // layer8. fillColor = nil;
  86. // layer8.strokeColor = [UIColor blueColor].CGColor;
  87. // layer8.path = path8.CGPath;
  88. // layer8.lineWidth = 10;
  89. // [self.layer addSublayer:layer8];
  90. }
  91. - (void)drawPointer{
  92. //画指针
  93. pointView = [[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-radius4/4, radius1-radius1+topMargin, radius4/2, radius1)];
  94. pointView.backgroundColor = _progressColor;
  95. [self addSubview:pointView];
  96. UIBezierPath *path7 = [UIBezierPath bezierPath];
  97. // [path7 moveToPoint:CGPointMake(self.center.x-radius4/4, self.center.y)];
  98. // [path7 addLineToPoint:CGPointMake(self.center.x+radius4/4, self.center.y)];
  99. // [path7 addLineToPoint:CGPointMake(self.center.x, self.center.y-radius1)];
  100. // [path7 closePath];
  101. [path7 moveToPoint:CGPointMake(pointView.frame.size.width/2, 0)];
  102. [path7 addLineToPoint:CGPointMake(0, pointView.frame.size.height)];
  103. [path7 addLineToPoint:CGPointMake(pointView.frame.size.width, pointView.frame.size.height)];
  104. [path7 closePath];
  105. CAShapeLayer *layer7 = [[CAShapeLayer alloc]init];
  106. layer7.frame = pointView.bounds;
  107. layer7.path = path7.CGPath;
  108. pointView.layer.mask = layer7;
  109. CGRect oldFrame = pointView.frame;
  110. pointView.layer.anchorPoint = CGPointMake(0.5, 1);
  111. pointView.frame = oldFrame;
  112. pointView.transform = CGAffineTransformMakeRotation(-angle/2);
  113. }
  114. - (void)drawLine{
  115. //内圆两边的线
  116. CGFloat startAngel = _start;
  117. CGFloat endAngel = startAngel+perAngle/5;//刻度线的宽度
  118. UIBezierPath *path5 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2,radius1+topMargin) radius:radius3+10 startAngle:startAngel endAngle:endAngel clockwise:YES];
  119. layer5 = [CAShapeLayer layer];
  120. layer5.strokeColor = [UIColor whiteColor].CGColor;
  121. layer5.lineWidth = 20;
  122. layer5.path = path5.CGPath;
  123. [self.layer addSublayer:layer5];
  124. CGFloat startAngel1 = _end-perAngle/5;
  125. CGFloat endAngel1 = startAngel1+perAngle/5;//刻度线的宽度
  126. UIBezierPath *path6 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2,radius1) radius:radius3+10 startAngle:startAngel1 endAngle:endAngel1 clockwise:YES];
  127. layer6 = [CAShapeLayer layer];
  128. layer6.strokeColor = [UIColor whiteColor].CGColor;
  129. layer6.lineWidth = 20;
  130. layer6.path = path6.CGPath;
  131. [self.layer addSublayer:layer6];
  132. }
  133. - (void)drawProgressCircle{
  134. //画内圆
  135. // UIBezierPath *path3 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2,radius1+topMargin) radius:radius3 startAngle:_start endAngle:_end clockwise:YES];
  136. // CAShapeLayer *layer3 = [[CAShapeLayer alloc]init];
  137. // layer3. fillColor = nil;
  138. // layer3.strokeColor = [UIColor whiteColor].CGColor;
  139. // layer3.path = path3.CGPath;
  140. // layer3.lineWidth = 1;
  141. // [self.layer addSublayer:layer3];
  142. //添加颜色渐变
  143. // gradientLayer2 = [[CAGradientLayer alloc]init];
  144. // gradientLayer2.colors = @[(__bridge id)[UIColor blueColor].CGColor, (__bridge id)[UIColor whiteColor].CGColor];
  145. // gradientLayer2.locations = @[@0.5,@1.0];
  146. // gradientLayer2.startPoint = CGPointMake(0, 0.5);
  147. // gradientLayer2.endPoint = CGPointMake(0.5, 1);
  148. // gradientLayer2.frame = self.bounds;
  149. // gradientLayer2.mask = layer3;
  150. // [self.layer addSublayer:gradientLayer2];
  151. //绘制进度
  152. // layer9 = [CAShapeLayer layer];
  153. // layer9.lineWidth = 1;
  154. // layer9.fillColor = [UIColor clearColor].CGColor;
  155. // layer9.strokeColor = _progressColor.CGColor;
  156. // layer9.path = path3.CGPath;
  157. // layer9.strokeStart = 0.0;
  158. // layer9.strokeEnd = 0.0;
  159. // layer9.lineCap=kCALineCapRound;
  160. //[self.layer addSublayer:layer9];
  161. }
  162. - (void)drawCircle{
  163. //绘制外围圆
  164. UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter: CGPointMake(self.frame.size.width/2,radius1+topMargin) radius:radius1 startAngle:M_PI*5/6 endAngle:M_PI/6 clockwise:YES];
  165. CAShapeLayer *layer1 = [[CAShapeLayer alloc]init];
  166. layer1. fillColor = nil;
  167. layer1.strokeColor = [UIColor whiteColor].CGColor;
  168. layer1.path = path1.CGPath;
  169. layer1.lineWidth = 10;
  170. [self.layer addSublayer:layer1];
  171. //添加颜色渐变
  172. CAGradientLayer *gradientLayer1 = [[CAGradientLayer alloc]init];
  173. gradientLayer1.colors = @[(__bridge id)[UIColor colorWithRed:70/255.0f green:116/255.0f blue:193/255.0f alpha:1].CGColor, (__bridge id)[UIColor colorWithRed:70/255.0f green:116/255.0f blue:193/255.0f alpha:1].CGColor,(__bridge id)[UIColor colorWithRed:70/255.0f green:116/255.0f blue:193/255.0f alpha:1].CGColor];
  174. gradientLayer1.locations = @[@0.01, @0.99, @1.0];
  175. gradientLayer1.startPoint = CGPointMake(0.5, 1);
  176. gradientLayer1.endPoint = CGPointMake(0.5, 0);
  177. gradientLayer1.frame = self.bounds;
  178. gradientLayer1.mask = layer1;
  179. [self.layer addSublayer:gradientLayer1];
  180. }
  181. //绘制刻度盘
  182. - (void)drawScale:(UIColor*)color AndNumber:(int)scaleN{
  183. for (int i = 0; i<_scaleNumber; i++) {
  184. CGFloat startAngel = _start+perAngle*i;
  185. CGFloat endAngel = startAngel + perAngle/10;//刻度线的宽度
  186. UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2,radius1+topMargin) radius:radius2 startAngle:startAngel endAngle:endAngel clockwise:YES];
  187. CAShapeLayer *layer2 = [CAShapeLayer layer];
  188. if (i<(scaleN+1)) {
  189. layer2.strokeColor = color.CGColor;
  190. }else{
  191. layer2.strokeColor =[UIColor colorWithRed:169/255.0 green:169/255.0 blue:169/255.0 alpha:1.0].CGColor;
  192. }
  193. if (i%10==0) {
  194. layer2.lineWidth = 15;
  195. CGPoint point = [self calculateTextPositonWithArcCenter:CGPointMake(self.frame.size.width/2,radius1+topMargin) Angle:startAngel];
  196. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(point.x-10, point.y-5, 18, 18)];
  197. label.text = [NSString stringWithFormat:@"%d",i/10*10];
  198. label.font = [UIFont systemFontOfSize:8];
  199. if(i<(scaleN+1)){
  200. label.textColor = [UIColor hexColor:greenColor];
  201. }
  202. else{
  203. label.textColor=[UIColor colorWithRed:169/255.0 green:169/255.0 blue:169/255.0 alpha:1.0];
  204. }
  205. label.textAlignment = NSTextAlignmentCenter;
  206. [self addSubview:label];
  207. }else{
  208. layer2.lineWidth = 7.5;
  209. }
  210. layer2.path = path2.CGPath;
  211. [self.layer addSublayer:layer2];
  212. [scaleLayer addObject:layer2];
  213. }
  214. }
  215. - (CGPoint)calculateTextPositonWithArcCenter:(CGPoint)center
  216. Angle:(CGFloat)angel
  217. {
  218. CGFloat x = (radius2-20) * cosf(angel);
  219. CGFloat y = (radius2-20) * sinf(angel);
  220. return CGPointMake(center.x + x, center.y + y);
  221. }
  222. - (void)progress:(CGFloat)value{
  223. CGFloat ang = angle*value/100-angle/2;
  224. NSLog(@"ang:%f",RADIANS_TO_DEGREES(ang));
  225. [UIView animateWithDuration:0.5 animations:^{
  226. layer9.strokeEnd = value/100;
  227. [self updateLineColor];
  228. [self updateScale:value];
  229. pointView.transform = CGAffineTransformMakeRotation(ang);
  230. }];
  231. backAngle = ang;
  232. }
  233. - (void)updateScale:(int)value{
  234. for (CAShapeLayer *layer in scaleLayer) {
  235. [layer removeFromSuperlayer];
  236. }
  237. [self drawScale:_progressColor AndNumber:value];
  238. }
  239. - (void)updateLineColor{
  240. if (layer9.strokeEnd>0) {
  241. layer5.strokeColor = _progressColor.CGColor;
  242. }else{
  243. layer5.strokeColor = [UIColor whiteColor].CGColor;
  244. }
  245. if (layer9.strokeEnd == 1.0) {
  246. layer6.strokeColor = _progressColor.CGColor;
  247. }else{
  248. layer6.strokeColor = [UIColor whiteColor].CGColor;
  249. }
  250. }
  251. @end