CCProgressView.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // CCProgressView.m
  3. // CCNavigationProgress
  4. //
  5. // Created by Marino Faggiana on 22/06/16.
  6. // Copyright (c) 2016 TWS. All rights reserved.
  7. //
  8. #import "CCProgressView.h"
  9. @interface CCProgressView ()
  10. @property (nonatomic, strong) UIView *progressBar;
  11. @end
  12. @implementation CCProgressView
  13. - (void)setProgress:(float)progress {
  14. _progress = (progress < 0) ? 0 :
  15. (progress > 1) ? 1 :
  16. progress;
  17. CGRect slice, remainder;
  18. CGRectDivide(self.bounds, &slice, &remainder, CGRectGetWidth(self.bounds) * _progress, CGRectMinXEdge);
  19. if (!CGRectEqualToRect(self.progressBar.frame, slice)) {
  20. self.progressBar.frame = slice;
  21. }
  22. }
  23. #pragma mark - UIView
  24. - (instancetype)initWithFrame:(CGRect)frame
  25. {
  26. if (self = [super initWithFrame:frame]) {
  27. self.frame = frame;
  28. self.clipsToBounds = YES;
  29. self.backgroundColor = [UIColor clearColor];
  30. self.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
  31. self.progressBar = [[UIView alloc] init];
  32. self.progressBar.backgroundColor = self.tintColor;
  33. self.progress = 0;
  34. [self addSubview:self.progressBar];
  35. }
  36. return self;
  37. }
  38. - (void)setFrame:(CGRect)frame
  39. {
  40. // 0.5 pt doesn't work well with autoresizingMask.
  41. frame.origin.y = ceilf(frame.origin.y);
  42. frame.size.height = floorf(frame.size.height);
  43. [super setFrame:frame];
  44. __weak typeof(self)weakSelf = self;
  45. dispatch_async(dispatch_get_main_queue(), ^{
  46. weakSelf.progress = weakSelf.progress;
  47. });
  48. }
  49. - (void)setTintColor:(UIColor *)tintColor
  50. {
  51. [super setTintColor:tintColor];
  52. self.progressBar.backgroundColor = tintColor;
  53. }
  54. @end