Skip to content

Instantly share code, notes, and snippets.

@advantis
Last active August 29, 2015 14:00
Show Gist options
  • Save advantis/11079377 to your computer and use it in GitHub Desktop.
Save advantis/11079377 to your computer and use it in GitHub Desktop.
Discrete slider
//
// Copyright © 2014 Yuri Kotov
//
#import <UIKit/UIKit.h>
@interface ADVDiscreteSlider : UISlider
@property (nonatomic) float unitValue;
@end
//
// Copyright © 2014 Yuri Kotov
//
#import "ADVDiscreteSlider.h"
static const float DefaultUnitValue = 1.f;
@interface ADVDiscreteSlider ()
@property (nonatomic) float recentValue;
@end
@implementation ADVDiscreteSlider
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
_unitValue = DefaultUnitValue;
}
return self;
}
- (id)initWithCoder:(NSCoder *)coder {
if ((self = [super initWithCoder:coder])) {
_unitValue = DefaultUnitValue;
}
return self;
}
- (void)setUnitValue:(float)unitValue {
NSParameterAssert(0 < unitValue);
_unitValue = unitValue;
}
- (void)setValue:(float)value animated:(BOOL)animated {
self.recentValue = self.value;
float ratio = 1. / self.unitValue;
value = roundf(ratio * value) / ratio;
[super setValue:value animated:animated];
}
- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
if (FLT_EPSILON < fabsf(self.value - self.recentValue)) {
[super sendAction:action to:target forEvent:event];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment