MJPhotoProgressView.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // MJPhotoProgressView.m
  3. //
  4. // Created by mj on 13-3-4.
  5. // Copyright (c) 2013年 itcast. All rights reserved.
  6. //
  7. #import "MJPhotoProgressView.h"
  8. #define kDegreeToRadian(x) (M_PI/180.0 * (x))
  9. @implementation MJPhotoProgressView
  10. - (id)initWithFrame:(CGRect)frame
  11. {
  12. if (self = [super initWithFrame:frame]) {
  13. self.backgroundColor = [UIColor clearColor];
  14. }
  15. return self;
  16. }
  17. - (void)drawRect:(CGRect)rect
  18. {
  19. CGPoint centerPoint = CGPointMake(rect.size.height / 2, rect.size.width / 2);
  20. CGFloat radius = MIN(rect.size.height, rect.size.width) / 2;
  21. CGFloat pathWidth = radius * 0.3f;
  22. CGFloat radians = kDegreeToRadian((_progress*359.9)-90);
  23. CGFloat xOffset = radius*(1 + 0.85*cosf(radians));
  24. CGFloat yOffset = radius*(1 + 0.85*sinf(radians));
  25. CGPoint endPoint = CGPointMake(xOffset, yOffset);
  26. CGContextRef context = UIGraphicsGetCurrentContext();
  27. [self.trackTintColor setFill];
  28. CGMutablePathRef trackPath = CGPathCreateMutable();
  29. CGPathMoveToPoint(trackPath, NULL, centerPoint.x, centerPoint.y);
  30. CGPathAddArc(trackPath, NULL, centerPoint.x, centerPoint.y, radius, kDegreeToRadian(270), kDegreeToRadian(-90), NO);
  31. CGPathCloseSubpath(trackPath);
  32. CGContextAddPath(context, trackPath);
  33. CGContextFillPath(context);
  34. CGPathRelease(trackPath);
  35. [self.progressTintColor setFill];
  36. CGMutablePathRef progressPath = CGPathCreateMutable();
  37. CGPathMoveToPoint(progressPath, NULL, centerPoint.x, centerPoint.y);
  38. CGPathAddArc(progressPath, NULL, centerPoint.x, centerPoint.y, radius, kDegreeToRadian(270), radians, NO);
  39. CGPathCloseSubpath(progressPath);
  40. CGContextAddPath(context, progressPath);
  41. CGContextFillPath(context);
  42. CGPathRelease(progressPath);
  43. CGContextAddEllipseInRect(context, CGRectMake(centerPoint.x - pathWidth/2, 0, pathWidth, pathWidth));
  44. CGContextFillPath(context);
  45. CGContextAddEllipseInRect(context, CGRectMake(endPoint.x - pathWidth/2, endPoint.y - pathWidth/2, pathWidth, pathWidth));
  46. CGContextFillPath(context);
  47. CGContextSetBlendMode(context, kCGBlendModeClear);;
  48. CGFloat innerRadius = radius * 0.7;
  49. CGPoint newCenterPoint = CGPointMake(centerPoint.x - innerRadius, centerPoint.y - innerRadius);
  50. CGContextAddEllipseInRect(context, CGRectMake(newCenterPoint.x, newCenterPoint.y, innerRadius*2, innerRadius*2));
  51. CGContextFillPath(context);
  52. }
  53. #pragma mark - Property Methods
  54. - (UIColor *)trackTintColor
  55. {
  56. if (!_trackTintColor)
  57. {
  58. _trackTintColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.7f];
  59. }
  60. return _trackTintColor;
  61. }
  62. - (UIColor *)progressTintColor
  63. {
  64. if (!_progressTintColor)
  65. {
  66. _progressTintColor = [UIColor whiteColor];
  67. }
  68. return _progressTintColor;
  69. }
  70. - (void)setProgress:(float)progress
  71. {
  72. _progress = progress;
  73. [self setNeedsDisplay];
  74. }
  75. @end