how to use the UISlider and how to set the slider

2019-05-05 08:04发布

i am using an UIslider first time. first i want to know how to gat value of slider positon if the range of value is 0 to 10:

second i want my slider just set at 5 different values . like 1, 2, 3, 4, 5

slider should not set between the labeled value enter image description here

3条回答
唯我独甜
2楼-- · 2019-05-05 08:22

first u set textfiled and add this function into code

-(IBAction)changeSlider:(id)sender 
{
    text_field .text= [[NSString alloc] initWithFormat:@" Value %d ", (int)slider.value];
}
查看更多
Deceive 欺骗
3楼-- · 2019-05-05 08:40

In this case I set the slider in XCode to have values of: 0-3. But the slider value is float: 0.000 - 3.000.

// **** Cylinder Volume Slider ****

- (NSInteger) normalizeValueCylinderVolumeSlider:(float) value
{
NSInteger normalizedValue = 0;

if (value>1.5)
{
    if (value>2.5)
        normalizedValue = 3;
    else
        normalizedValue = 2;
}
else if (value>0.5)
    normalizedValue = 1;

return normalizedValue;

}

- (IBAction)valueChangedCylinderVolumeSlider:(UISlider *)sender
{
float value = [sender value];
NSInteger normalizedValue;

// Step 1:

normalizedValue = [self normalizeValueCylinderVolumeSlider:value];

// Step 2:

if (normalizedValue>value)
{
    if ((normalizedValue-value)<0.3)
        sender.value = normalizedValue;
}
else if ((value-normalizedValue)<0.3)
    sender.value = normalizedValue;

}

- (IBAction)touchUpInsideCylinderVolumeSlider:(UISlider *)sender
{
float normalizedValue = 0;

normalizedValue = [self normalizeValueCylinderVolumeSlider:[sender value]];

sender.value = normalizedValue;

}

- (IBAction)touchUpOutsideCylinderVolumeSlider:(UISlider *)sender
{
float normalizedValue = 0;

normalizedValue = [self normalizeValueCylinderVolumeSlider:[sender value]];

sender.value = normalizedValue;

}


On valueChanged action, (step 1) I set the value to rigid values: 0, 1, 2, 3. And (step 2) I stick the slider thumb to the rigid values if it get close by 0.3 from the rigid value.

On touchUpInside & touchUpOutside I repeat 'step 1' code from valueChanged action to fix the slider value for the case that the drag action end outside the stick range ('step 2').


The best is to put all this code in a sub-class of Slider. But this is my only first week / attempt on iOS development.

查看更多
叛逆
4楼-- · 2019-05-05 08:42

I followed this tutorial for mine and it was very helpful there are variables you can set in the code such as max number and how much the slider increases each time. Link: http://www.xprogress.com/post-35-uislider-tutorial-example-how-to-use-slider-in-iphone-sdk-xcode/

查看更多
登录 后发表回答