UGCKitCircleProgressView.m 4.4 KB

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