Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @interface KPSmallStatsView ()
- @property (nonatomic, assign) CGFloat maxValue;
- @property (nonatomic, assign) CGFloat minValue;
- @property (nonatomic, assign) CGFloat currentValue;
- @property (nonatomic, assign) CGFloat usableTrackLenght;
- @property (nonatomic, assign) CGFloat cursorWidth;
- @property (nonatomic, strong) UIView *trackLine;
- @end
- @implementation KPSmallStatsView
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = [UIColor clearColor];
- self.maxValue = 30;
- self.minValue = 18;
- self.currentValue = 25.7;
- //---- TRACK LINE
- _trackLine = [[UIView alloc] initWithFrame:CGRectZero];
- _trackLine.backgroundColor = [self trackLineColor];
- _trackLine.translatesAutoresizingMaskIntoConstraints = NO;
- [self addSubview:_trackLine];
- [_trackLine.topAnchor constraintEqualToAnchor:mediaTitle.bottomAnchor constant:25].active = YES;
- [_trackLine.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;
- [_trackLine.heightAnchor constraintEqualToConstant:1].active = YES;
- self.trackLineWidthConstraint = [_trackLine.widthAnchor constraintEqualToAnchor:self.widthAnchor multiplier:0.8];
- self.trackLineWidthConstraint.active = YES;
- }
- return self;
- }
- -(CGFloat)positionForValue:(CGFloat)value {
- return self.usableTrackLenght * (value - self.minValue) / (self.maxValue - self.minValue) + (self.cursorWidth /2);
- }
- -(void)layoutSubviews {
- [super layoutSubviews];
- NSLog(@"FRAME %f", self.trackLine.frame.size.width);
- //---- CURSOR ( Thumb)
- self.cursorWidth = 41;
- self.usableTrackLenght = self.trackLine.frame.size.width - self.cursorWidth;
- CGFloat cursorCenter = [self positionForValue:self.currentValue];
- UIView *cursor = [[UIView alloc] init];
- cursor.backgroundColor = [UIColor colorWithHexString:@"#E66163" setAlpha:.2];
- cursor.layer.borderColor = [UIColor colorWithHexString:@"#E66163" setAlpha:1].CGColor;
- cursor.layer.borderWidth = 1;
- cursor.layer.cornerRadius = 4;
- cursor.translatesAutoresizingMaskIntoConstraints = NO;
- [self.trackLine addSubview:cursor];
- CGFloat cursorPosition = cursorCenter - self.cursorWidth / 2;
- [cursor.centerYAnchor constraintEqualToAnchor:self.trackLine.centerYAnchor].active = YES;
- [cursor.leftAnchor constraintEqualToAnchor:self.trackLine.leftAnchor constant: cursorPosition].active = YES;
- [cursor.widthAnchor constraintEqualToConstant:self.cursorWidth].active = YES;
- [cursor.heightAnchor constraintEqualToConstant:self.cursorWidth -13].active = YES;
- //---- MEDIA VALUE LABEL
- UILabel *mediaValue = [[UILabel alloc] init];
- mediaValue.textColor = [UIColor whiteColor];
- mediaValue.alpha = 1;
- mediaValue.textAlignment = NSTextAlignmentCenter;
- mediaValue.font = [UIFont defaultAppFontWithSize:13 style:KPFontStyleSemiBold];
- mediaValue.text = [NSString stringWithFormat:@"%0.1f", self.currentValue];
- mediaValue.translatesAutoresizingMaskIntoConstraints = NO;
- [cursor addSubview:mediaValue];
- [mediaValue.centerYAnchor constraintEqualToAnchor:cursor.centerYAnchor constant:1].active = YES;
- [mediaValue.centerXAnchor constraintEqualToAnchor:cursor.centerXAnchor].active = YES;
- }
Add Comment
Please, Sign In to add comment