xcode 如何使用 UISlider 以及如何在特定值上设置滑块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11609992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use the UISlider and how to set the slider on a particular values?
提问by Faheem Rajput
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:
我第一次使用 UIslider。首先,如果值的范围是 0 到 10,我想知道如何设置滑块位置的值:
second i want my slider just set at 5 different values . like 1, 2, 3, 4, 5
其次,我希望我的滑块设置为 5 个不同的值。像 1, 2, 3, 4, 5
slider should not set between the labeled value
滑块不应设置在标记值之间
采纳答案by Bradley
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/
我遵循了我的本教程,它非常有帮助,您可以在代码中设置变量,例如最大数量和滑块每次增加的数量。链接:http: //www.xprogress.com/post-35-uislider-tutorial-example-how-to-use-slider-in-iphone-sdk-xcode/
回答by vinothsivanandam
first u set textfiled and add this function into code
首先你设置 textfiled 并将这个函数添加到代码中
-(IBAction)changeSlider:(id)sender
{
text_field .text= [[NSString alloc] initWithFormat:@" Value %d ", (int)slider.value];
}
回答by Amit G.
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.
在这种情况下,我将 XCode 中的滑块设置为:0-3。但滑块值是浮动的: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.
在 valueChanged 操作中,(第 1 步)我将值设置为刚性值:0、1、2、3。并且(第 2 步)如果滑块与刚性值接近 0.3,我将其贴在刚性值上。
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').
在 touchUpInside 和 touchUpOutside 上,我重复 valueChanged 动作中的“步骤 1”代码,以修复拖动动作在操纵杆范围外结束的情况下的滑块值(“步骤 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.
最好是将所有这些代码放在 Slider 的一个子类中。但这是我唯一的第一周/尝试 iOS 开发。