我有一个 UISlider,我想将它的值设置为 1 到 10。我使用的代码是。
let slider = UISlider()
slider.value = 1.0
// This works I know that
slider.value = 10.0
我想要做的是为 UISlider 设置动画,以便它需要 0.5 秒才能更改。我不希望它变得更加平滑。
目前我的想法是。
let slider = UISlider()
slider.value = 1.0
// This works I know that
UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animation: { slider.value = 10.0 } completion: nil)
我正在寻找 Swift 中的解决方案。
最佳答案
已编辑
经过一些讨论,我想我应该澄清两个建议的解决方案之间的区别:
- 使用内置的 UISlider 方法
.setValue(10.0, animated: true)
。 - 将此方法封装在
UIView.animateWithDuration
中。
由于作者明确要求进行需要 0.5 秒的更改——可能由另一个操作触发——因此第二种解决方案是 prefer。
例如,考虑一个按钮连接到一个将 slider 设置为其最大值的 Action 。
@IBOutlet weak var slider: UISlider!
@IBAction func buttonAction(sender: AnyObject) {
// Method 1: no animation in this context
slider.setValue(10.0, animated: true)
// Method 2: animates the transition, ok!
UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animations: {
self.slider.setValue(10.0, animated: true) },
completion: nil)
}
运行一个简单的单个 UIVIewController
应用程序,仅包含 UISlider
和 UIButton
对象会产生以下结果。
关于ios - UISlider 设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34315534/