我有一个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中寻找解决方案。
发布于 2015-12-16 23:19:02
编辑的
经过一些讨论,我想我应该澄清两个建议的解决方案之间的区别:
在UIView.animateWithDuration.中使用.setValue(10.0, animated: true).
由于作者明确地要求更改需要0.5秒-可能是由另一个操作触发的-第二种解决方案是首选。
例如,假设一个按钮连接到将滑块设置为其最大值的操作。
@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)
}仅使用UISlider和UIButton对象运行一个简单的UIVIewController应用程序会产生以下结果。
animated: true)animated: false,则转换将为instantaneous.发布于 2016-03-01 13:37:46
@dfri的答案的问题是蓝色的Minimum Tracker正在从100%移动到这个值,所以为了解决这个问题,你需要稍微改变一下方法:
extension UISlider
{
///EZSE: Slider moving to value with animation duration
public func setValue(value: Float, duration: Double) {
UIView.animateWithDuration(duration, animations: { () -> Void in
self.setValue(self.value, animated: true)
}) { (bol) -> Void in
UIView.animateWithDuration(duration, animations: { () -> Void in
self.setValue(value, animated: true)
}, completion: nil)
}
}
}https://stackoverflow.com/questions/34315534
复制相似问题