YMCircleView.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // YMCircleView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/2/25.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMCircleView.h"
  9. @implementation YMCircleView
  10. - (instancetype)initWithFrame:(CGRect)frame{
  11. if (self = [super initWithFrame:frame]) {
  12. self.backgroundColor = UIColor.clearColor;
  13. //默认颜色
  14. self.progerssColor = UIColor.blueColor;
  15. self.progerssBackgroundColor = UIColor.lightGrayColor;
  16. //默认进度条宽度
  17. self.progerWidth = 15;
  18. self.progerBackgroundWidth = 0;
  19. }
  20. return self;
  21. }
  22. - (void)setProgress:(CGFloat)progress{
  23. _progress = progress;
  24. [self setNeedsDisplay];
  25. }
  26. - (void)drawRect:(CGRect)rect{
  27. //路径
  28. UIBezierPath *backgroundPath = [[UIBezierPath alloc] init];
  29. //线宽
  30. backgroundPath.lineWidth = self.progerBackgroundWidth;
  31. //颜色
  32. [self.progerssBackgroundColor set];
  33. //拐角
  34. backgroundPath.lineCapStyle = kCGLineCapRound;
  35. backgroundPath.lineJoinStyle = kCGLineJoinRound;
  36. CGFloat borderScale = (self.progerWidth - self.progerBackgroundWidth) / 2.0f;
  37. //半径
  38. CGFloat radius = (MIN(rect.size.width - borderScale, rect.size.height - borderScale) - self.progerBackgroundWidth) * 0.5;
  39. //画弧(参数:中心、半径、起始角度(3点钟方向为0)、结束角度、是否顺时针)
  40. [backgroundPath addArcWithCenter:(CGPoint){rect.size.width * 0.5, rect.size.height * 0.5} radius:radius startAngle:M_PI * 1.5 endAngle:M_PI * 1.5 + M_PI * 2 clockwise:YES];
  41. //连线
  42. [backgroundPath stroke];
  43. //路径
  44. UIBezierPath *progressPath = [[UIBezierPath alloc] init];
  45. //线宽
  46. progressPath.lineWidth = self.progerWidth;
  47. //颜色
  48. [self.progerssColor set];
  49. //拐角
  50. progressPath.lineCapStyle = kCGLineCapRound;
  51. progressPath.lineJoinStyle = kCGLineJoinRound;
  52. CGFloat radius1 = (MIN(rect.size.width, rect.size.height) - self.progerWidth) * 0.5;
  53. //画弧(参数:中心、半径、起始角度(3点钟方向为0)、结束角度、是否顺时针)
  54. [progressPath addArcWithCenter:(CGPoint){rect.size.width * 0.5, rect.size.height * 0.5} radius:radius1 startAngle:M_PI * 1.5 endAngle:M_PI * 1.5 + M_PI * 2 * _progress clockwise:YES];
  55. //连线
  56. [progressPath stroke];
  57. }
  58. @end