我有这两个按钮,如果我按下左边的按钮,我希望滑块一直缓慢向左移动,当我按下右边的按钮一直移动滑块时一样在右边。我现在的代码现在非常快速地移动滑块,我希望它能逐渐移动。
override func didMoveToView(view: SKView) {
middleSlider = UISlider(frame: CGRectMake(self.view!.bounds.width/2 - 150/2, self.view!.bounds.height/2 * 1.7, 150, 50))
middleSlider.tintColor = UIColor.whiteColor()
middleSlider.minimumTrackTintColor = UIColor.whiteColor()
middleSlider.maximumTrackTintColor = UIColor.whiteColor()
middleSlider.setThumbImage(UIImage(named: "mixerb1"), forState: UIControlState.Normal)
middleSlider.maximumValue = 2.0
middleSlider.minimumValue = 0.0
middleSlider.value = 1.0
middleSlider.continuous = true
middleSlider.userInteractionEnabled = true
middleSlider.addTarget(self, action: #selector(middleSliderChangeAudio), forControlEvents: UIControlEvents.AllEvents)
self.view?.addSubview(middleSlider)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
if node.name == "left" {
print("slider moves to left")
UIView.animateWithDuration(10.0, animations: {
self.middleSlider.value = self.middleSlider.minimumValue
self.middleSlider.continuous = true
}
)
}
if node.name == "right" {
print("slider moves to right")
UIView.animateWithDuration(10.0, animations: {
self.middleSlider.value = self.middleSlider.maximumValue
self.middleSlider.continuous = true
}
)
}
}
答案 0 :(得分:4)
在这里使用setValue(_:animated :)方法Documentation。 让我们知道它是怎么回事。
一旦你添加了这个,就把它包装在你的动画中,持续时间如下。请注意,10秒只是一个硬编码值,将持续时间更改为您想要的任何值,同样会转到您想要设置的值。
@IBAction func moveSlider(sender: UIButton) {
UIView.animateWithDuration(10, animations: {
self.slider.setValue(1.0, animated: true)
})
}
在您熟悉它的同时,向上滚动一下以查看属性值的文档。因此,您可以更好地了解正在发生的事情。