TCCircleProgressView.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // TCCircleProgressView.m
  3. // TXXiaoShiPinDemo
  4. //
  5. // Created by linkzhzhu on 2017/12/12.
  6. // Copyright © 2017年 tencent. All rights reserved.
  7. //
  8. #import "TCCircleProgressView.h"
  9. @implementation TCCircleProgressView
  10. - (id)initWithFrame:(CGRect)frame{
  11. self = [super initWithFrame:frame];
  12. if (self) {
  13. self.backgroundColor = [UIColor clearColor];
  14. _percent = 0;
  15. _width = 0;
  16. }
  17. return self;
  18. }
  19. - (void)setPercent:(float)percent{
  20. _percent = percent;
  21. [self setNeedsDisplay];
  22. }
  23. - (void)drawRect:(CGRect)rect{
  24. [self addArcBackColor];
  25. [self drawArc];
  26. [self addCenterBack];
  27. [self addCenterLabel];
  28. }
  29. - (void)addArcBackColor{
  30. CGColorRef color = (_arcBackColor == nil) ? [UIColor lightGrayColor].CGColor : _arcBackColor.CGColor;
  31. CGContextRef contextRef = UIGraphicsGetCurrentContext();
  32. CGSize viewSize = self.bounds.size;
  33. CGPoint center = CGPointMake(viewSize.width / 2, viewSize.height / 2);
  34. CGFloat radius = viewSize.width / 2;
  35. CGContextBeginPath(contextRef);
  36. CGContextMoveToPoint(contextRef, center.x, center.y);
  37. CGContextAddArc(contextRef, center.x, center.y, radius,0,2*M_PI, 0);
  38. CGContextSetFillColorWithColor(contextRef, color);
  39. CGContextFillPath(contextRef);
  40. }
  41. - (void)drawArc{
  42. if (_percent == 0 || _percent > 1) {
  43. return;
  44. }
  45. if (_percent == 1) {
  46. CGColorRef color = (_arcFinishColor == nil) ? [UIColor greenColor].CGColor : _arcFinishColor.CGColor;
  47. CGContextRef contextRef = UIGraphicsGetCurrentContext();
  48. CGSize viewSize = self.bounds.size;
  49. CGPoint center = CGPointMake(viewSize.width / 2, viewSize.height / 2);
  50. // Draw the slices.
  51. CGFloat radius = viewSize.width / 2;
  52. CGContextBeginPath(contextRef);
  53. CGContextMoveToPoint(contextRef, center.x, center.y);
  54. CGContextAddArc(contextRef, center.x, center.y, radius,0,2*M_PI, 0);
  55. CGContextSetFillColorWithColor(contextRef, color);
  56. CGContextFillPath(contextRef);
  57. }
  58. else{
  59. float endAngle = 2*M_PI*_percent;
  60. CGColorRef color = (_arcUnfinishColor == nil) ? [UIColor blueColor].CGColor : _arcUnfinishColor.CGColor;
  61. CGContextRef contextRef = UIGraphicsGetCurrentContext();
  62. CGSize viewSize = self.bounds.size;
  63. CGPoint center = CGPointMake(viewSize.width / 2, viewSize.height / 2);
  64. // Draw the slices.
  65. CGFloat radius = viewSize.width / 2;
  66. CGContextBeginPath(contextRef);
  67. CGContextMoveToPoint(contextRef, center.x, center.y);
  68. CGContextAddArc(contextRef, center.x, center.y, radius,0,endAngle, 0);
  69. CGContextSetFillColorWithColor(contextRef, color);
  70. CGContextFillPath(contextRef);
  71. }
  72. }
  73. -(void)addCenterBack{
  74. float width = (_width == 0) ? 5 : _width;
  75. CGColorRef color = (_centerColor == nil) ? [UIColor whiteColor].CGColor : _centerColor.CGColor;
  76. CGContextRef contextRef = UIGraphicsGetCurrentContext();
  77. CGSize viewSize = self.bounds.size;
  78. CGPoint center = CGPointMake(viewSize.width / 2, viewSize.height / 2);
  79. // Draw the slices.
  80. CGFloat radius = viewSize.width / 2 - width;
  81. CGContextBeginPath(contextRef);
  82. CGContextMoveToPoint(contextRef, center.x, center.y);
  83. CGContextAddArc(contextRef, center.x, center.y, radius,0,2*M_PI, 0);
  84. CGContextSetFillColorWithColor(contextRef, color);
  85. CGContextFillPath(contextRef);
  86. }
  87. - (void)addCenterLabel{
  88. NSString *percent = @"";
  89. float fontSize = 14;
  90. UIColor *arcColor = [UIColor blueColor];
  91. if (_percent == 1) {
  92. percent = @"100";
  93. fontSize = 14;
  94. arcColor = (_arcFinishColor == nil) ? [UIColor greenColor] : _arcFinishColor;
  95. }else if(_percent < 1 && _percent >= 0){
  96. fontSize = 13;
  97. arcColor = (_arcUnfinishColor == nil) ? [UIColor blueColor] : _arcUnfinishColor;
  98. percent = [NSString stringWithFormat:@"%d",(int)(_percent*100)];
  99. }
  100. CGSize viewSize = self.bounds.size;
  101. NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
  102. paragraph.alignment = NSTextAlignmentCenter;
  103. NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:fontSize],NSFontAttributeName,arcColor,NSForegroundColorAttributeName,[UIColor clearColor],NSBackgroundColorAttributeName,paragraph,NSParagraphStyleAttributeName,nil];
  104. [percent drawInRect:CGRectMake(5, (viewSize.height-fontSize)/2, viewSize.width-10, fontSize)withAttributes:attributes];
  105. }
  106. @end