UGCKitPieProgressView.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright © 2019 Tencent. All rights reserved.
  2. #import "UGCKitPieProgressView.h"
  3. @implementation UGCKitPieProgressView
  4. - (instancetype)initWithFrame:(CGRect)frame
  5. {
  6. self = [super initWithFrame:frame];
  7. if (self) {
  8. [self commonInit];
  9. }
  10. return self;
  11. }
  12. - (instancetype)initWithCoder:(NSCoder *)coder
  13. {
  14. self = [super initWithCoder:coder];
  15. if (self) {
  16. [self commonInit];
  17. }
  18. return self;
  19. }
  20. - (void)commonInit {
  21. self.contentMode = UIViewContentModeRedraw;
  22. self.tintColor = [UIColor cyanColor];
  23. }
  24. - (void)setProgress:(float)progress
  25. {
  26. if (_progress != progress) {
  27. _progress = progress;
  28. [self setNeedsDisplay];
  29. }
  30. }
  31. - (void)setTintColor:(UIColor *)tintColor {
  32. if (_tintColor != tintColor) {
  33. _tintColor = tintColor;
  34. [self setNeedsDisplay];
  35. }
  36. }
  37. - (void)drawRect:(CGRect)rect {
  38. CGContextRef context = UIGraphicsGetCurrentContext();
  39. [self.tintColor set];
  40. // Draw background
  41. CGFloat lineWidth = 2.f;
  42. CGRect allRect = self.bounds;
  43. CGRect circleRect = CGRectInset(allRect, lineWidth/2.f, lineWidth/2.f);
  44. CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
  45. CGContextSetLineWidth(context, lineWidth);
  46. CGContextStrokeEllipseInRect(context, circleRect);
  47. // 90 degrees
  48. CGFloat startAngle = - ((float)M_PI / 2.f);
  49. // Draw progress
  50. UIBezierPath *processPath = [UIBezierPath bezierPath];
  51. processPath.lineCapStyle = kCGLineCapButt;
  52. processPath.lineWidth = lineWidth * 2.f;
  53. CGFloat radius = (CGRectGetWidth(self.bounds) / 2.f) - (processPath.lineWidth / 2.f) - 3.f;
  54. CGFloat endAngle = (self.progress * 2.f * (float)M_PI) + startAngle;
  55. [processPath moveToPoint:center];
  56. [processPath moveToPoint:CGPointMake(center.x, radius)];
  57. [processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
  58. [processPath closePath];
  59. // Ensure that we don't get color overlaping when _progressTintColor alpha < 1.f.
  60. CGContextSetBlendMode(context, kCGBlendModeCopy);
  61. [processPath fill];
  62. }
  63. @end