CCProgressView.m 1.5 KB

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