PKPendingView.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // PKPendingView.m
  3. // Download
  4. //
  5. // Created by Pavel on 30/05/15.
  6. // Copyright (c) 2015 Katunin. All rights reserved.
  7. //
  8. #import "PKPendingView.h"
  9. #import "NSLayoutConstraint+PKDownloadButton.h"
  10. #import "CALayer+PKDownloadButtonAnimations.h"
  11. static NSString *const kSpinAnimationKey = @"PKSpin";
  12. static const CGFloat kDefaultRaduis = 13.f;
  13. static const CGFloat kDefaultEmptyLineRadians = 0.4f;
  14. static const CGFloat kDefaultSpinTime = 1.f;
  15. @interface PKPendingView ()
  16. @property (nonatomic, weak) PKCircleView *circleView;
  17. @property (nonatomic, weak) NSLayoutConstraint *width;
  18. @property (nonatomic, weak) NSLayoutConstraint *height;
  19. @property (nonatomic, assign) BOOL isSpinning;
  20. - (PKCircleView *)createCircleView;
  21. - (NSArray *)createConstraints;
  22. @end
  23. static PKPendingView *CommonInit(PKPendingView *self) {
  24. if (self != nil) {
  25. PKCircleView *circleView = [self createCircleView];
  26. circleView.translatesAutoresizingMaskIntoConstraints = NO;
  27. [self addSubview:circleView];
  28. self.circleView = circleView;
  29. self.circleView.userInteractionEnabled = NO;
  30. [self addConstraints:[self createConstraints]];
  31. self.emptyLineRadians = kDefaultEmptyLineRadians;
  32. self.radius = kDefaultRaduis;
  33. self.clipsToBounds = NO;
  34. self.spinTime = kDefaultSpinTime;
  35. [self startSpin];
  36. }
  37. return self;
  38. }
  39. @implementation PKPendingView
  40. #pragma mark - initialization
  41. - (id)initWithCoder:(NSCoder *)decoder {
  42. return CommonInit([super initWithCoder:decoder]);
  43. }
  44. - (instancetype)initWithFrame:(CGRect)frame {
  45. return CommonInit([super initWithFrame:frame]);
  46. }
  47. #pragma mark - properties
  48. - (void)setSpinTime:(CGFloat)spinTime {
  49. _spinTime = spinTime;
  50. [self.circleView.layer removeRotationAnimationWithKey:kSpinAnimationKey];
  51. if (self.isSpinning) {
  52. [self startSpin];
  53. }
  54. }
  55. - (void)setRadius:(CGFloat)radius {
  56. self.width.constant = radius * 2;
  57. self.height.constant = radius * 2;
  58. [self setNeedsLayout];
  59. }
  60. - (void)setLineWidth:(CGFloat)lineWidth {
  61. self.circleView.lineWidth = lineWidth;
  62. [self setNeedsDisplay];
  63. }
  64. - (CGFloat)lineWidth {
  65. return self.circleView.lineWidth;
  66. }
  67. - (void)setEmptyLineRadians:(CGFloat)emptyLineRadians {
  68. _emptyLineRadians = emptyLineRadians;
  69. self.circleView.startAngleRadians = 1.5f * M_PI + emptyLineRadians / 2.f;
  70. self.circleView.endAngleRadians = self.circleView.startAngleRadians + 2 * M_PI - emptyLineRadians;
  71. [self setNeedsDisplay];
  72. }
  73. - (void)setTintColor:(UIColor *)tintColor {
  74. self.circleView.tintColor = tintColor;
  75. [self setNeedsDisplay];
  76. }
  77. #pragma mark - private methods
  78. - (PKCircleView *)createCircleView {
  79. PKCircleView *circleView = [[PKCircleView alloc] init];
  80. NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintForView:circleView
  81. withHeight:0.f];
  82. NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintForView:circleView
  83. withWidth:0.f];
  84. [circleView addConstraints:@[heightConstraint, widthConstraint]];
  85. self.width = widthConstraint;
  86. self.height = heightConstraint;
  87. return circleView;
  88. }
  89. - (NSArray *)createConstraints {
  90. NSMutableArray *constraints = [NSMutableArray array];
  91. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsForWrappedSubview:self.circleView
  92. withInsets:UIEdgeInsetsZero]];
  93. return constraints;
  94. }
  95. - (void)startSpin {
  96. self.isSpinning = YES;
  97. [self.circleView.layer addRotationAnimationWithKey:kSpinAnimationKey
  98. fullRotationDuration:self.spinTime];
  99. }
  100. - (void)stopSpin {
  101. [self.circleView.layer removeRotationAnimationWithKey:kSpinAnimationKey];
  102. self.isSpinning = NO;
  103. }
  104. @end