PKCircleView.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // PKCircleView.m
  3. // Download
  4. //
  5. // Created by Pavel on 30/05/15.
  6. // Copyright (c) 2015 Katunin. All rights reserved.
  7. //
  8. #import "PKCircleView.h"
  9. static const CGFloat kDefaultLineWidth = 1.f;
  10. @interface PKCircleView ()
  11. - (void)drawCircleRadius:(CGFloat)radius
  12. rect:(CGRect)rect
  13. startAngle:(CGFloat)startAngle
  14. endAngle:(CGFloat)endAngel
  15. lineWidth:(CGFloat)lineWidth;
  16. @end
  17. static PKCircleView *CommonInit(PKCircleView *self) {
  18. if (self != nil) {
  19. self.backgroundColor = [UIColor clearColor];
  20. self.startAngleRadians = M_PI * 1.5;
  21. self.endAngleRadians = self.startAngleRadians + (M_PI * 2);
  22. self.lineWidth = kDefaultLineWidth;
  23. }
  24. return self;
  25. }
  26. @implementation PKCircleView
  27. #pragma mark - initialization
  28. - (id)initWithCoder:(NSCoder *)decoder {
  29. return CommonInit([super initWithCoder:decoder]);
  30. }
  31. - (instancetype)initWithFrame:(CGRect)frame {
  32. return CommonInit([super initWithFrame:frame]);
  33. }
  34. #pragma mark - properties
  35. - (void)setLineWidth:(CGFloat)lineWidth {
  36. _lineWidth = lineWidth;
  37. [self setNeedsDisplay];
  38. }
  39. - (void)setStartAngleRadians:(CGFloat)startAngleRadians {
  40. _startAngleRadians = startAngleRadians;
  41. [self setNeedsDisplay];
  42. }
  43. - (void)setEndAngleRadians:(CGFloat)endAngleRadians {
  44. _endAngleRadians = endAngleRadians;
  45. [self setNeedsDisplay];
  46. }
  47. #pragma mark - UIView
  48. - (void)drawRect:(CGRect)rect {
  49. [self drawCircleRadius:MIN(rect.size.width / 2, rect.size.height / 2) - self.lineWidth / 2.f
  50. rect:rect
  51. startAngle:self.startAngleRadians
  52. endAngle:self.endAngleRadians
  53. lineWidth:self.lineWidth];
  54. }
  55. #pragma mark - private methods
  56. - (void)drawCircleRadius:(CGFloat)radius
  57. rect:(CGRect)rect
  58. startAngle:(CGFloat)startAngle
  59. endAngle:(CGFloat)endAngel
  60. lineWidth:(CGFloat)lineWidth {
  61. UIBezierPath* bezierPath = [UIBezierPath bezierPath];
  62. [self.tintColor setStroke];
  63. [bezierPath addArcWithCenter:CGPointMake(rect.size.width / 2, rect.size.height / 2)
  64. radius:radius
  65. startAngle:startAngle
  66. endAngle:endAngel
  67. clockwise:YES];
  68. bezierPath.lineWidth = lineWidth;
  69. [bezierPath stroke];
  70. }
  71. @end