ZJCircleProgressView.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // ZJCircleProgressView.m
  3. // ZJCircleProgressView
  4. //
  5. // Created by ZeroJ on 16/10/16.
  6. // Copyright © 2016年 ZeroJ. All rights reserved.
  7. //
  8. #import "ZJCircleProgressView.h"
  9. static CGFloat radiusFromAngle(CGFloat angle) {
  10. return (angle * M_PI)/180;
  11. }
  12. @interface ZJCircleProgressView ()
  13. @property (strong, nonatomic) UILabel *progressLabel;
  14. @property (strong, nonatomic) UIImageView *imageView;
  15. @end
  16. @implementation ZJCircleProgressView
  17. - (instancetype)init {
  18. if (self = [super init]) {
  19. [self commonInit];
  20. }
  21. return self;
  22. }
  23. - (instancetype)initWithFrame:(CGRect)frame {
  24. if (self = [super initWithFrame:frame]) {
  25. [self commonInit];
  26. }
  27. return self;
  28. }
  29. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  30. if (self = [super initWithCoder:aDecoder]) {
  31. [self commonInit];
  32. }
  33. return self;
  34. }
  35. - (void)commonInit {
  36. _trackBackgroundColor = [UIColor lightGrayColor];
  37. _trackColor = [UIColor blueColor];
  38. _lineWidth = 10;
  39. _lineCap = kCGLineCapRound;
  40. _beginAngle = radiusFromAngle(-90);
  41. // self.clipsToBounds = NO;
  42. // self.layer.masksToBounds = YES;
  43. self.backgroundColor = [UIColor clearColor];
  44. [self addSubview:self.progressLabel];
  45. self.progressLabel.hidden = YES;
  46. }
  47. - (void)layoutSubviews {
  48. [super layoutSubviews];
  49. CGFloat slideLength = self.bounds.size.width;
  50. // 大概设置为内部小圆的外接正方形 -- 准确的位置实际应该先计算出label的高度来设置的
  51. // 不过不用精确设置也能正确显示
  52. self.progressLabel.frame = CGRectMake(_lineWidth, _lineWidth, slideLength-2*_lineWidth, slideLength-2*_lineWidth);
  53. if (self.headerImage) {
  54. CGFloat angle = _progress*2*M_PI + _beginAngle;
  55. CGFloat centerX = slideLength/2;
  56. CGFloat centerY = slideLength/2;
  57. CGFloat r = (self.bounds.size.width-_lineWidth)/2;
  58. CGFloat imageCenterX = centerX + r*cos(angle);
  59. CGFloat imageCenterY = centerY + r*sin(angle);
  60. // 这里 + 8 作为imageView比轨道宽8的效果
  61. self.imageView.frame = CGRectMake(0, 0, _lineWidth+8, _lineWidth+8);
  62. self.imageView.center = CGPointMake(imageCenterX, imageCenterY);
  63. }
  64. }
  65. // Only override drawRect: if you perform custom drawing.
  66. // An empty implementation adversely affects performance during animation.
  67. - (void)drawRect:(CGRect)rect {
  68. // 获取当前上下文
  69. CGContextRef currentContext = UIGraphicsGetCurrentContext();
  70. CGFloat slideLength = self.bounds.size.width;
  71. CGFloat centerX = slideLength/2;
  72. CGFloat centerY = slideLength/2;
  73. // 添加背景轨道
  74. CGContextAddArc(currentContext, centerX, centerY, (slideLength-_lineWidth)/2, 0, 2*M_PI, 0);
  75. // 设置轨道宽度
  76. CGContextSetLineWidth(currentContext, self.lineWidth);
  77. // 设置背景颜色
  78. [self.trackBackgroundColor setStroke];
  79. // 绘制轨道
  80. CGContextStrokePath(currentContext);
  81. // 进度条轨道
  82. CGFloat deltaAngle = _progress*2*M_PI;
  83. // 根据进度progress的值绘制进度条
  84. // 注意: 角度需要使用弧度制
  85. // 设置圆心x, y坐标
  86. // 设置圆的半径 -- 这里需要的自然是(边长-轨道宽度)/2
  87. // 从beginAngle 绘制到endAngle= beginAngle+deltaAngle;
  88. CGContextAddArc(currentContext, centerX, centerY, (slideLength-_lineWidth)/2, self.beginAngle, self.beginAngle+deltaAngle, self.clickWise);
  89. // 设置进度条颜色
  90. [self.trackColor setStroke];
  91. // 设置轨道宽度
  92. CGContextSetLineWidth(currentContext, _lineWidth);
  93. // 设置轨道端点的样式
  94. CGContextSetLineCap(currentContext, _lineCap);
  95. // 使用stroke方式填充路径
  96. CGContextStrokePath(currentContext);
  97. // CGFloat r = (self.bounds.size.width-_lineWidth)/2;
  98. // CGFloat imageCenterX = centerX + r*cos(deltaAngle+_beginAngle);
  99. // CGFloat imageCenterY = centerY + r*sin(deltaAngle+_beginAngle);
  100. // CGFloat imageWidth = _lineWidth+8;
  101. // CGRect imageRect = CGRectMake(0, 0, imageWidth, imageWidth);
  102. // imageRect.origin.x = imageCenterX - imageWidth/2;
  103. // imageRect.origin.y = imageCenterY - imageWidth/2;
  104. //
  105. // CGContextDrawImage(currentContext, imageRect, [self.imageView.image CGImage]);
  106. }
  107. - (void)setProgress:(CGFloat)progress {
  108. if (progress > 1 || progress < 0) return;
  109. _progress = progress;
  110. self.progressLabel.text = [NSString stringWithFormat:@"%.1f%%", progress*100];
  111. // 标记为需要重新绘制, 将会在下一个绘制循环中, 调用drawRect:方法重新绘制
  112. [self setNeedsDisplay];
  113. if (self.headerImage) { // 需要显示图片就触发重新布局
  114. [self setNeedsLayout];
  115. }
  116. }
  117. // 重写headerImage set方法, 如果设置了图片就添加imageVIew
  118. - (void)setHeaderImage:(UIImage *)headerImage {
  119. _headerImage = headerImage;
  120. if (self.headerImage) {
  121. [self addSubview:self.imageView];
  122. self.imageView.image = self.headerImage;
  123. }
  124. }
  125. - (UIImageView *)imageView {
  126. if (!_imageView) {
  127. UIImageView *imageView = [[UIImageView alloc] init];
  128. imageView.layer.masksToBounds = YES;
  129. imageView.clipsToBounds = YES;
  130. imageView.layer.cornerRadius = 9;
  131. imageView.backgroundColor = [UIColor blueColor];
  132. _imageView = imageView;
  133. }
  134. return _imageView;
  135. }
  136. - (UILabel *)progressLabel {
  137. if (!_progressLabel) {
  138. UILabel *progressLabel = [[UILabel alloc] init];
  139. progressLabel.textColor = self.trackColor;
  140. progressLabel.backgroundColor = [UIColor clearColor];
  141. progressLabel.textAlignment = NSTextAlignmentCenter;
  142. progressLabel.text = @"0.0%";
  143. _progressLabel = progressLabel;
  144. }
  145. return _progressLabel;
  146. }
  147. @end