Tagging for UISlider

Is there a way to set labels for UISlider. NSSlider has something called - (void)setNumberOfTickMarks:(NSInteger)numberOfTickMarks . But UISlider does not seem to have this.

What I want to do is get the values ​​of the slider as 0,1,2,3,4 etc. if, say, I set min as 0 and max as 5. Now, since such a UISLider returns something like 0.0.0.1,0.2, 0.3, etc. 4,8,4,9,5,0 based on the above example.

Any help?

+4
source share
4 answers

UISlider has no tags.

To get the behavior you specified, you have to round the number returned by the slider to the nearest integer.

So, when you start to slide, and the slider reports a value of 0.1 or 0.2, round it to 0. Until it reaches 0.5, when you round to 1.

Then, when the slider stops sliding, you can set its value with the number to which you have rounded, which will make the slider “tied” to this position, like snapping to a tick mark.

If you need visual markings, you will have to implement this yourself.

+5
source

Yes, I think subclassing is the way to go. This is what I did:

TickSlider.h ...

 /** * A custom slider that allows the user to specify the number of tick marks. * It behaves just like the NSSlider with tick marks for a MacOS app. **/ @interface TickSlider : UISlider /** * The number of tickmarks for the slider. No graphics will be draw in this * sublcass but the value of the slider will be rounded to the nearest tick * mark in the same way/behaviour as the NSSlider on a MacOS app. * @discussion This value can be set as a UserDefinedAttribute in the xib file. **/ @property (nonatomic) NSInteger numberOfTickMarks; @end 

TickSlider.m ...

 #import "TickSlider.h" @implementation TickSlider - (void)setValue:(float)value animated:(BOOL)animated { float updatedValue = value; if (self.numberOfTickMarks > 0) { updatedValue = MAX(self.minimumValue, MIN(value, self.maximumValue)); if (self.numberOfTickMarks == 1) { updatedValue = (self.minimumValue + self.maximumValue) * 0.5f; } else { const int kNumberOfDivisions = (self.numberOfTickMarks - 1); const float kSliderRange = ([self maximumValue] - [self minimumValue]); const float kRangePerDivision = (kSliderRange / kNumberOfDivisions); const float currentTickMark = roundf(value / kRangePerDivision); updatedValue = (currentTickMark * kRangePerDivision); } } [super setValue:updatedValue animated:animated]; } @end 
+3
source

As above, casting to int causes the slider to behave as if you were sliding next to your thumb. The above code code is interesting, allowing you to specify more "stop values" (ticks) as the value of the maximum value.

If you need a simple “integer slider”, rounding (value + 0.25) actually makes the slider behave pretty well. This is the code I'm using:

 @interface IntegerSlider : UISlider @end @implementation IntegerSlider - (void) setValue: (float) value animated: (BOOL) animated { value = roundf(value + 0.25); [super setValue: value animated: animated]; } 

@end

+1
source

I just used [slider value] for int for this.

0
source

Source: https://habr.com/ru/post/1305752/


All Articles