PKCircleProgressView.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // PKCircleProgressView.m
  3. // PKDownloadButton
  4. //
  5. // Created by Pavel on 28/05/15.
  6. // Copyright (c) 2015 Katunin. All rights reserved.
  7. //
  8. #import "PKCircleProgressView.h"
  9. #import "PKCircleView.h"
  10. #import "NSLayoutConstraint+PKDownloadButton.h"
  11. static const CGFloat kDefaultRaduis = 13.f;
  12. static const CGFloat kDefaultFilledLineWidth = 3.f;
  13. static const CGFloat kDefaultEmptyLineWidth = 1.f;
  14. static const CGFloat kStartAngle = M_PI * 1.5;
  15. @interface PKCircleProgressView ()
  16. @property (nonatomic, assign) CGFloat startAngle;
  17. @property (nonatomic, assign) CGFloat endAngle;
  18. @property (nonatomic, weak) PKCircleView *emptyLineCircleView;
  19. @property (nonatomic, weak) PKCircleView *filledLineCircleView;
  20. @property (nonatomic, weak) NSLayoutConstraint *emptyLineCircleWidth;
  21. @property (nonatomic, weak) NSLayoutConstraint *emptyLineCircleHeight;
  22. @property (nonatomic, weak) NSLayoutConstraint *filledLineCircleWidth;
  23. @property (nonatomic, weak) NSLayoutConstraint *filledLineCircleHeight;
  24. @property (nonatomic, assign) CGFloat emptyLineCircleSize;
  25. @property (nonatomic, assign) CGFloat filledLineCircleSize;
  26. - (PKCircleView *)createEmptyLineCircleView;
  27. - (PKCircleView *)createFilledLineCircleView;
  28. - (NSArray *)createCircleConstraints;
  29. @end
  30. static PKCircleProgressView *CommonInit(PKCircleProgressView *self) {
  31. if (self != nil) {
  32. self.backgroundColor = [UIColor clearColor];
  33. self.startAngle = kStartAngle;
  34. self.endAngle = self.startAngle + (M_PI * 2);
  35. self.clipsToBounds = NO;
  36. PKCircleView *emptyLineCircleView = [self createEmptyLineCircleView];
  37. self.emptyLineCircleView = emptyLineCircleView;
  38. emptyLineCircleView.translatesAutoresizingMaskIntoConstraints = NO;
  39. [self addSubview:emptyLineCircleView];
  40. PKCircleView *filledLineCircleView = [self createFilledLineCircleView];
  41. self.filledLineCircleView = filledLineCircleView;
  42. filledLineCircleView.translatesAutoresizingMaskIntoConstraints = NO;
  43. [self addSubview:filledLineCircleView];
  44. [self addConstraints:[self createCircleConstraints]];
  45. self.emptyLineWidth = kDefaultEmptyLineWidth;
  46. self.filledLineWidth = kDefaultFilledLineWidth;
  47. self.radius = kDefaultRaduis;
  48. }
  49. return self;
  50. }
  51. @implementation PKCircleProgressView
  52. #pragma mark - initilaization / deallocation
  53. - (id)initWithCoder:(NSCoder *)decoder {
  54. return CommonInit([super initWithCoder:decoder]);
  55. }
  56. - (instancetype)initWithFrame:(CGRect)frame {
  57. return CommonInit([super initWithFrame:frame]);
  58. }
  59. #pragma mark - properties
  60. - (void)setEmptyLineCircleSize:(CGFloat)emptyLineCircleSize {
  61. self.emptyLineCircleWidth.constant = emptyLineCircleSize;
  62. self.emptyLineCircleHeight.constant = emptyLineCircleSize;
  63. }
  64. - (void)setFilledLineCircleSize:(CGFloat)filledLineCircleSize {
  65. self.filledLineCircleWidth.constant = filledLineCircleSize;
  66. self.filledLineCircleHeight.constant = filledLineCircleSize;
  67. }
  68. - (void)setProgress:(CGFloat)progress {
  69. _progress = progress;
  70. self.filledLineCircleView.startAngleRadians = self.startAngle;
  71. self.filledLineCircleView.endAngleRadians = (self.endAngle - self.startAngle) * progress + self.startAngle;
  72. [self setNeedsDisplay];
  73. }
  74. - (void)setFilledLineWidth:(CGFloat)filledLineWidth {
  75. _filledLineWidth = filledLineWidth;
  76. self.filledLineCircleView.lineWidth = filledLineWidth;
  77. [self setNeedsUpdateConstraints];
  78. }
  79. - (void)setEmptyLineWidth:(CGFloat)emptyLineWidth {
  80. _emptyLineWidth = emptyLineWidth;
  81. self.emptyLineCircleView.lineWidth = emptyLineWidth;
  82. [self setNeedsUpdateConstraints];
  83. }
  84. - (void)setRadius:(CGFloat)radius {
  85. _radius = radius;
  86. [self setNeedsUpdateConstraints];
  87. }
  88. - (void)setFilledLineStyleOuter:(BOOL)filledLineStyleOuter {
  89. _filledLineStyleOuter = filledLineStyleOuter;
  90. [self setNeedsUpdateConstraints];
  91. }
  92. #pragma mark - UIView
  93. - (void)updateConstraints {
  94. [super updateConstraints];
  95. self.emptyLineCircleSize = self.radius * 2.f;
  96. CGFloat deltaRaduis = 0.f;
  97. if (self.filledLineStyleOuter) {
  98. deltaRaduis = - self.emptyLineCircleView.lineWidth / 2.f + self.filledLineCircleView.lineWidth;
  99. }
  100. else {
  101. deltaRaduis = - self.emptyLineCircleView.lineWidth / 2.f;
  102. }
  103. self.filledLineCircleSize = self.radius * 2.f + deltaRaduis * 2.f;
  104. }
  105. #pragma mark - private methods
  106. - (PKCircleView *)createEmptyLineCircleView {
  107. PKCircleView *emptyCircelView = [[PKCircleView alloc] init];
  108. NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintForView:emptyCircelView
  109. withWidth:0.f];
  110. self.emptyLineCircleWidth = widthConstraint;
  111. [emptyCircelView addConstraint:widthConstraint];
  112. NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintForView:emptyCircelView
  113. withHeight:0.f];
  114. self.emptyLineCircleHeight = heightConstraint;
  115. [emptyCircelView addConstraint:heightConstraint];
  116. return emptyCircelView;
  117. }
  118. - (PKCircleView *)createFilledLineCircleView {
  119. PKCircleView *filledCircelView = [[PKCircleView alloc] init];
  120. NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintForView:filledCircelView
  121. withWidth:0.f];
  122. self.filledLineCircleWidth = widthConstraint;
  123. [filledCircelView addConstraint:widthConstraint];
  124. NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintForView:filledCircelView
  125. withHeight:0.f];
  126. self.filledLineCircleHeight = heightConstraint;
  127. [filledCircelView addConstraint:heightConstraint];
  128. return filledCircelView;
  129. }
  130. - (NSArray *)createCircleConstraints {
  131. NSMutableArray *constraints = [NSMutableArray array];
  132. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsForCenterView:self.emptyLineCircleView
  133. withView:self]];
  134. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsForWrappedSubview:self.filledLineCircleView
  135. withInsets:UIEdgeInsetsZero]];
  136. return constraints;
  137. }
  138. @end