Guest User

Untitled

a guest
Oct 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. @interface KPSmallStatsView ()
  2. @property (nonatomic, assign) CGFloat maxValue;
  3. @property (nonatomic, assign) CGFloat minValue;
  4. @property (nonatomic, assign) CGFloat currentValue;
  5. @property (nonatomic, assign) CGFloat usableTrackLenght;
  6. @property (nonatomic, assign) CGFloat cursorWidth;
  7. @property (nonatomic, strong) UIView *trackLine;
  8. @end
  9.  
  10. @implementation KPSmallStatsView
  11.  
  12. - (instancetype)initWithFrame:(CGRect)frame {
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. self.backgroundColor = [UIColor clearColor];
  16.  
  17. self.maxValue = 30;
  18. self.minValue = 18;
  19. self.currentValue = 25.7;
  20.  
  21. //---- TRACK LINE
  22. _trackLine = [[UIView alloc] initWithFrame:CGRectZero];
  23. _trackLine.backgroundColor = [self trackLineColor];
  24. _trackLine.translatesAutoresizingMaskIntoConstraints = NO;
  25. [self addSubview:_trackLine];
  26.  
  27. [_trackLine.topAnchor constraintEqualToAnchor:mediaTitle.bottomAnchor constant:25].active = YES;
  28. [_trackLine.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;
  29. [_trackLine.heightAnchor constraintEqualToConstant:1].active = YES;
  30. self.trackLineWidthConstraint = [_trackLine.widthAnchor constraintEqualToAnchor:self.widthAnchor multiplier:0.8];
  31. self.trackLineWidthConstraint.active = YES;
  32.  
  33. }
  34. return self;
  35. }
  36.  
  37. -(CGFloat)positionForValue:(CGFloat)value {
  38. return self.usableTrackLenght * (value - self.minValue) / (self.maxValue - self.minValue) + (self.cursorWidth /2);
  39. }
  40.  
  41. -(void)layoutSubviews {
  42. [super layoutSubviews];
  43.  
  44. NSLog(@"FRAME %f", self.trackLine.frame.size.width);
  45.  
  46. //---- CURSOR ( Thumb)
  47. self.cursorWidth = 41;
  48.  
  49. self.usableTrackLenght = self.trackLine.frame.size.width - self.cursorWidth;
  50.  
  51. CGFloat cursorCenter = [self positionForValue:self.currentValue];
  52.  
  53. UIView *cursor = [[UIView alloc] init];
  54. cursor.backgroundColor = [UIColor colorWithHexString:@"#E66163" setAlpha:.2];
  55. cursor.layer.borderColor = [UIColor colorWithHexString:@"#E66163" setAlpha:1].CGColor;
  56. cursor.layer.borderWidth = 1;
  57. cursor.layer.cornerRadius = 4;
  58. cursor.translatesAutoresizingMaskIntoConstraints = NO;
  59. [self.trackLine addSubview:cursor];
  60.  
  61. CGFloat cursorPosition = cursorCenter - self.cursorWidth / 2;
  62.  
  63. [cursor.centerYAnchor constraintEqualToAnchor:self.trackLine.centerYAnchor].active = YES;
  64. [cursor.leftAnchor constraintEqualToAnchor:self.trackLine.leftAnchor constant: cursorPosition].active = YES;
  65. [cursor.widthAnchor constraintEqualToConstant:self.cursorWidth].active = YES;
  66. [cursor.heightAnchor constraintEqualToConstant:self.cursorWidth -13].active = YES;
  67.  
  68. //---- MEDIA VALUE LABEL
  69. UILabel *mediaValue = [[UILabel alloc] init];
  70. mediaValue.textColor = [UIColor whiteColor];
  71. mediaValue.alpha = 1;
  72. mediaValue.textAlignment = NSTextAlignmentCenter;
  73. mediaValue.font = [UIFont defaultAppFontWithSize:13 style:KPFontStyleSemiBold];
  74. mediaValue.text = [NSString stringWithFormat:@"%0.1f", self.currentValue];
  75. mediaValue.translatesAutoresizingMaskIntoConstraints = NO;
  76. [cursor addSubview:mediaValue];
  77.  
  78. [mediaValue.centerYAnchor constraintEqualToAnchor:cursor.centerYAnchor constant:1].active = YES;
  79. [mediaValue.centerXAnchor constraintEqualToAnchor:cursor.centerXAnchor].active = YES;
  80. }
Add Comment
Please, Sign In to add comment