Setting interval value for UISlider

2019-01-25 11:42发布

I need to set the interval value for an UISlider.

Please help..

6条回答
劳资没心,怎么记你
2楼-- · 2019-01-25 12:17

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楼-- · 2019-01-25 12:21

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

查看更多
冷血范
4楼-- · 2019-01-25 12:23

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;
查看更多
可以哭但决不认输i
5楼-- · 2019-01-25 12:27

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;
    }
}
查看更多
Bombasti
6楼-- · 2019-01-25 12:27
int sliderValue =  kSliderInterval * floor((slider.value / kSliderInterval) + 0.5);
查看更多
别忘想泡老子
7楼-- · 2019-01-25 12:28

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);
}
查看更多
登录 后发表回答