PKStopDownloadButton.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // PKStopDownloadButton.m
  3. // PKDownloadButton
  4. //
  5. // Created by Pavel on 28/05/15.
  6. // Copyright (c) 2015 Katunin. All rights reserved.
  7. //
  8. #import "PKStopDownloadButton.h"
  9. #import "NSLayoutConstraint+PKDownloadButton.h"
  10. #import "UIImage+PKDownloadButton.h"
  11. static const CGFloat kDefaultStopButtonWidth = 8.f;
  12. @interface PKStopDownloadButton ()
  13. @property (nonatomic, weak) UIButton *stopButton;
  14. - (UIButton *)createStopButton;
  15. - (NSArray *)createStopButtonConstraints;
  16. - (void)updateAppearance;
  17. - (PKCircleProgressView *)createCircleProgressView;
  18. @end
  19. static PKStopDownloadButton *CommonInit(PKStopDownloadButton *self) {
  20. if (self != nil) {
  21. UIButton *stopButton = [self createStopButton];
  22. stopButton.translatesAutoresizingMaskIntoConstraints = NO;
  23. [self addSubview:stopButton];
  24. self.stopButton = stopButton;
  25. [self addConstraints:[self createStopButtonConstraints]];
  26. [self updateAppearance];
  27. [self setNeedsDisplay];
  28. }
  29. return self;
  30. }
  31. @implementation PKStopDownloadButton
  32. #pragma mark - properties
  33. - (void)setStopButtonWidth:(CGFloat)stopButtonWidth {
  34. _stopButtonWidth = stopButtonWidth;
  35. [self.stopButton setImage:[UIImage stopImageOfSize:stopButtonWidth
  36. color:self.tintColor]
  37. forState:UIControlStateNormal];
  38. [self setNeedsDisplay];
  39. }
  40. #pragma mark - initialization
  41. - (instancetype)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 - private methods
  48. - (UIButton *)createStopButton {
  49. UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
  50. stopButton.tintColor = [UIColor clearColor];
  51. _stopButtonWidth = kDefaultStopButtonWidth;
  52. return stopButton;
  53. }
  54. - (NSArray *)createStopButtonConstraints {
  55. NSMutableArray *constraints = [NSMutableArray array];
  56. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsForWrappedSubview:self.stopButton
  57. withInsets:UIEdgeInsetsZero]];
  58. return constraints;
  59. }
  60. - (PKCircleProgressView *)createCircleProgressView {
  61. PKCircleProgressView *circleProgressView = [[PKCircleProgressView alloc] init];
  62. return circleProgressView;
  63. }
  64. #pragma mark - appearance
  65. - (void)updateAppearance {
  66. [self.stopButton setImage:[UIImage stopImageOfSize:_stopButtonWidth color:self.tintColor]
  67. forState:UIControlStateNormal];
  68. }
  69. - (void)tintColorDidChange {
  70. [super tintColorDidChange];
  71. [self updateAppearance];
  72. }
  73. @end