ios 增量为 5 的 UISlider
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2519460/
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
UISlider with increments of 5
提问by Tyler29294
How do I have my UISlider
go from 1-100
in increments of 5
?
我怎么有我的UISlider
去从1-100
,增量5
?
回答by Tuomas Pelkonen
Add a target like this:
添加这样的目标:
slider.continuous = YES;
[slider addTarget:self
action:@selector(valueChanged:)
forControlEvents:UIControlEventValueChanged];
And in the valueChanged function set the value to the closest value that is divisible by 5.
在 valueChanged 函数中,将值设置为最接近的可被 5 整除的值。
[slider setValue:((int)((slider.value + 2.5) / 5) * 5) animated:NO];
So if you need any interval other than 5 simply set it like so:
因此,如果您需要 5 以外的任何间隔,只需像这样设置:
float interval = 5.0f;//set this
[slider setValue:interval*floorf((slider.value/interval)+0.5f) animated:NO];
回答by Jure
A slightly more elegant solution in Swift could be something like this
Swift 中稍微更优雅的解决方案可能是这样的
let step: Float = 5
@IBAction func sliderValueChanged(sender: UISlider) {
let roundedValue = round(sender.value / step) * step
sender.value = roundedValue
// Do something else with the value
}
This way you get steps of 5. You can read more about the setup in my post.
回答by MB.
There is another way to active the stepper functionality. works in a general case
还有另一种激活步进器功能的方法。在一般情况下工作
Example:
例子:
range of data is 0 - 2800 and I want the increments to be in 25 unit values. I would do the following
数据范围是 0 - 2800,我希望增量为 25 个单位值。我会做以下
- set up the range of the slider to be 0 - (maxRange/unitsteps).
- when the value changed method runs use (int)slider.value * unitsteps .
- 将滑块的范围设置为 0 - (maxRange/unitsteps)。
- 当 value changed 方法运行时使用 (int)slider.value * unitsteps 。
in the example above my slider would be set in IB to range 0 - 112. and my code would be (int)slider.value * 25.
在上面的示例中,我的滑块将在 IB 中设置为范围 0 - 112。我的代码将是 (int)slider.value * 25。
if you then have a text field that can be modified for direct input you would just do the opposite set the slider value to the [textfield.text intValue]/ unitsteps.
如果您有一个可以修改为直接输入的文本字段,您只需将滑块值设置为 [textfield.text intValue]/unitsteps。
回答by Justin Domnitz
For anyone looking for the Swift syntax...
对于任何寻找 Swift 语法的人...
override func viewDidLoad() {
super.viewDidLoad()
mySlider.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged)
}
func sliderValueDidChange(sender:UISlider!)
{
//Set value to the nearest 5...
print((Float)((Int)((sender.value + 2.5) / 5) * 5))
sender.setValue((Float)((Int)((sender.value + 2.5) / 5) * 5), animated: false)
}
Alternatively, just create an Action function with the Assistant Editor (what I did ultimately)...
或者,只需使用助理编辑器创建一个动作功能(我最终做了什么)......
@IBAction func mySliderValueChanged(sender: UISlider) {
//Set value to the nearest int
sender.setValue(Float(roundf(sender.value)), animated: false)
}
回答by Javier Calatrava Llavería
In swift 3, slider with increments +/- 7:
在 swift 3 中,滑块增量为 +/- 7:
@IBOutlet weak var sldComponent: UISlider!
...
...
sldComponent.addTarget(self, action: #selector(_sldComponentChangedValue),for: .valueChanged)
...
...
func _sldComponentChangedValue(sender: UISlider!) {
self.sldComponent.setValue((round(sender.value / 7) * 7), animated: false)
print("\(sender.value)")
}
回答by LARA Ann
1. In storyboard mode, drag and drop a stepper and label. the label will show the stepper values.
- left click and drag both label and stepper to your view controller as outlets.
- 左键单击并将标签和步进器作为出口拖动到您的视图控制器。
name label as "stepperValue"
将标签命名为“stepperValue”
- drag and drop stepper again making it a func- I called mine "func"
- 再次拖放步进器使其成为功能 - 我称我的为“功能”
@IBAction func inc(sender: AnyObject) {
stepperValue.text = "(Int(stepper.value) * 5 )"
@IBAction func inc(sender: AnyObject) {
stepperValue.text = "(Int(stepper.value) * 5 )"
click the stepper and it will increment by 5
单击步进器,它将增加 5