how to use the UISlider and how to set the slider

2019-05-05 08:35发布

问题:

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

回答1:

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/



回答2:

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];
}


回答3:

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.