FUCircleProgressView.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // CircleProgressView.m
  3. // ProgressView
  4. //
  5. // Created by zhao on 16/9/13.
  6. // Copyright © 2016年 zhaoName. All rights reserved.
  7. // 正常的圆形进度条
  8. #import "FUCircleProgressView.h"
  9. #define WIDTH self.frame.size.width
  10. #define HEIGHT self.frame.size.height
  11. @implementation FUCircleProgressView
  12. - (instancetype)initWithFrame:(CGRect)frame {
  13. self = [super initWithFrame:frame];
  14. if(self) {
  15. [self initData];
  16. }
  17. return self;
  18. }
  19. - (instancetype)init {
  20. self = [super init];
  21. if(self) {
  22. [self initData];
  23. }
  24. return self;
  25. }
  26. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  27. self = [super initWithCoder:aDecoder];
  28. if(self) {
  29. [self initData];
  30. }
  31. return self;
  32. }
  33. /** 初始化数据*/
  34. - (void)initData
  35. {
  36. self.progressWidth = 3.0;
  37. self.progressColor = [UIColor redColor];
  38. self.progressBackgroundColor = [UIColor grayColor];
  39. self.percent = 0.0;
  40. self.clockwise =0;
  41. self.labelbackgroundColor = [UIColor clearColor];
  42. self.textColor = [UIColor blackColor];
  43. self.textFont = [UIFont systemFontOfSize:15];
  44. }
  45. - (void)layoutSubviews
  46. {
  47. [super addSubview:self.centerLabel];
  48. self.centerLabel.backgroundColor = self.labelbackgroundColor;
  49. self.centerLabel.textColor = self.textColor;
  50. self.centerLabel.font = self.textFont;
  51. [self addSubview:self.centerLabel];
  52. }
  53. #pragma mark -- 画进度条
  54. - (void)drawRect:(CGRect)rect
  55. {
  56. //获取当前画布
  57. CGContextRef context = UIGraphicsGetCurrentContext();
  58. CGContextSetShouldAntialias(context, YES);
  59. CGContextAddArc(context, WIDTH/2, HEIGHT/2, (WIDTH-self.progressWidth)/2, 0, M_PI*2, 0);
  60. [self.progressBackgroundColor setStroke];//设置圆描边背景的颜色
  61. //画线的宽度
  62. CGContextSetLineWidth(context, self.progressWidth);
  63. //绘制路径
  64. CGContextStrokePath(context);
  65. if(self.percent)
  66. {
  67. CGFloat angle = 2 * self.percent * M_PI - M_PI_2;
  68. if(self.clockwise) {//反方向
  69. CGContextAddArc(context, WIDTH/2, HEIGHT/2, (WIDTH-self.progressWidth)/2, ((int)self.percent == 1 ? -M_PI_2 : angle), -M_PI_2, 0);
  70. }
  71. else {//正方向
  72. CGContextAddArc(context, WIDTH/2, HEIGHT/2, (WIDTH-self.progressWidth)/2, -M_PI_2, angle, 0);
  73. }
  74. [self.progressColor setStroke];//设置圆描边的颜色
  75. CGContextSetLineWidth(context, self.progressWidth);
  76. CGContextStrokePath(context);
  77. }
  78. }
  79. - (void)setPercent:(float)percent
  80. {
  81. if(self.percent < 0) return;
  82. _percent = percent;
  83. [self setNeedsDisplay];
  84. }
  85. - (UILabel *)centerLabel
  86. {
  87. if(!_centerLabel)
  88. {
  89. _centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/2)];
  90. _centerLabel.center = CGPointMake(WIDTH/2, HEIGHT/2);
  91. _centerLabel.textAlignment = NSTextAlignmentCenter;
  92. }
  93. return _centerLabel;
  94. }
  95. @end