Swift如何自己搭建UISlider自动滑动?

How to build the UISlider automatic slide by itself in Swift?

我有意设置 UISlider 将自动滑动以在视频播放器中使用。 我试图通过研究找到解决方案并遵循该代码,但它仍然不起作用。每个人都有解决办法,这是我的代码。

override func awakeFromNib() {
      self.sldTime.maximumValue = 10
      self.sldTime.minimumValue = 0
      self.sldTime.continuous = true
      self.sldTime.addTarget(self, action: #selector(self.sliderAutoChange), forControlEvents: .ValueChanged)
}

func sliderAutoChange() {
    let index = Float(self.sldTime.value + 1)   // increase by 1
    self.sldTime.setValue(index, animated: false)
    print("index \(index)")
} 

当我调试它时,sliderAutoChange() 函数似乎没有调用。

如果您想自动更改 Slider 值,那么您可以像这样使用 NSTimer

override func awakeFromNib() {
    self.sldTime.maximumValue = 10
    self.sldTime.minimumValue = 0
    self.sldTime.continuous = true

    NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(self.updateSlider(_:), userInfo: nil, repeats: true)      
}

func updateSlider(timer: NSTimer) {

    let index = Float(self.sldTime.value + 1)   // increase by 1
    self.sldTime.setValue(index, animated: true)
    print("index \(index)")
    if self.sldTime.value >= self.sldTime.maximumValue {
          timer.invalidate()
    }
}