I need to set the interval value for an UISlider.
Please help..
问题:
回答1:
yourSlider.value = x;
You should really read the documentation, lots of great resources in the iOS Developer Center.
Edit: With regards to your more specific question in the comments:
yourSlider.minimumValue = -10;
yourSlider.maximumValue = 10;
[yourSlider addTarget:self action:@selector(roundValue) forControlEvents:UIControlEventValueChanged];
- (void)roundValue{
yourSlider.value = round(yourSlider.value);
}
回答2:
A flexible solution:
// define MIN_VALUE, MAX_VALUE and INTERVAL somewhere, such as 3, 18 and 3
slider.TouchUpInside += delegate {
touchUpInside();
};
/// <summary>
/// set value to one of intervals
/// </summary>
void touchUpInside(){
// get the nearest interval value
for(int i=MIN_VALUE; i<=MAX_VALUE; i=i+INVERVAL){
if(slider.Value > i && slider.Value < i + INVERVAL){
if(slider.Value - i < i + INVERVAL - slider.Value){
slider.Value = i;
}else{
slider.Value = i + INVERVAL;
}
break;
}
}
}
回答3:
I know this question is old but I just want to share how I did this.
Assuming that the UISlider object has already been initialized, first, the minimum and maximum values of the UISlider object must be set:
mySlider.minimumValue = -2.0;
mySlider.maximumValue = 2.0;
Then, set the selector that will handle the changes in the slider:
[mySlider addTarget:self action:@selector(sliderDidChangeValue:) forControlEvents:UIControlEventValueChanged];
Also, set the interval (to a property). It is important that the interval is positive. (And, really, the INTERVAL MUST BE POSITIVE.)
self.interval = 0.5 //_interval is float
Declare the method(s):
- (void)sliderDidChangeValue:(id)sender
{
UISlider *slider = (UISlider *)sender;
//Round the value to the a target interval
CGFloat roundedValue = [self roundValue:slider.value];
//Snap to the final value
[slider setValue:roundedValue animated:NO];
}
- (float)roundValue:(float)value
{
//get the remainder of value/interval
//make sure that the remainder is positive by getting its absolute value
float tempValue = fabsf(fmodf(value, _interval)); //need to import <math.h>
//if the remainder is greater than or equal to the half of the interval then return the higher interval
//otherwise, return the lower interval
if(tempValue >= (_interval / 2.0)){
return value - tempValue + _interval;
}
else{
return value - tempValue;
}
}
回答4:
int sliderValue = kSliderInterval * floor((slider.value / kSliderInterval) + 0.5);
回答5:
UISliders
do not 'increment' their value, they increase or decrease their value according to how they're touched. You can use these properties to set the slider limits:
slider.minimumValue = 0.0;
slider.maximumValue = 0.5;
回答6:
i would suggest is that put the level just below the slider where you want to put value and set maximum and minimum value then the in between value can be calculated by no of point and by division and finally display in uilabel the values. hope this will help
good luck