ios - UISlider 设置值

标签 ios swift user-interface uislider uianimation

我有一个 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 中的解决方案。

最佳答案

已编辑

经过一些讨论,我想我应该澄清两个建议的解决方案之间的区别:

  1. 使用内置的 UISlider 方法 .setValue(10.0, animated: true)
  2. 将此方法封装在 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 应用程序,仅包含 UISliderUIButton 对象会产生以下结果。

  • Method 1 : 即时幻灯片(即使 animated: true)
  • Method 2 :动画过渡。请注意,如果我们在此上下文中设置 animated: false,则转换将是即时的。

关于ios - UISlider 设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34315534/

相关文章:

ios - 计算总行驶距离 iOS Swift

javascript - html 元素 safari iOS 的高度 100%

ios - 在 iOS Swift 中设置按钮事件颜色的简单方法

android - 为特定 View 创建用户说明

安卓扭曲的用户界面

ios - swift 2.x : searching the node tree for multiple hits

objective-c - 仅在特定设备上部署 iOS

html - 滚动行为平滑, anchor 标记没有区别

arrays - 在 Swift 2.2 中附加数组时使用 'subscript' 不明确

Swift Eureka Forms - 如何为字段选项赋值?