LZCircleView.m 2.2 KB

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