Last active
August 29, 2015 14:00
-
-
Save advantis/11079377 to your computer and use it in GitHub Desktop.
Discrete slider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Copyright © 2014 Yuri Kotov | |
// | |
#import <UIKit/UIKit.h> | |
@interface ADVDiscreteSlider : UISlider | |
@property (nonatomic) float unitValue; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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