快速点击按钮启动计时器
问题描述:
我快速创建了一个计时器,以便在按下按钮时一次又一次地将 UISlider 从一端移动到另一端.但是我总是在计时器线上得到一个断点,尽管一切都应该是正确的.
Ive created a timer in swift to move a UISlider from one end to another again and again when a button is pressed. But I'm always getting a breakpoint at the timer line, although everything should be right.
@IBAction func setSliderValue(_ sender: UIButton){
mytimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
}
func timerAction(){
let Range = slider.maximumValue - slider.minimumValue;
let Increment = Range/100;
let newval = slider.value + Increment;
if(Increment >= slider.maximumValue)
{
slider.setValue(newval, animated: true)
}
else
{
slider.setValue(0, animated: true)
}
}
答
它现在可以使用以下代码,尽管滑块只会从左向右移动,直到它失效.
Its working now with the following code, though the slider is only moving from left to right until it gets invalidated.
@IBAction func setSliderValue(_ sender: UIButton){
mytimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
/*slider.setValue(100, animated: true)
print("The value of the slider is now \(slider.value)")
sliderValue = Int(slider.value)*/
}
func timerAction(){
let Range = slider.maximumValue - slider.minimumValue;
let Increment = Range/100;
let newval = slider.value + Increment;
if(Increment <= slider.maximumValue)
{
slider.setValue(newval, animated: true)
print("The value of the slider is now \(slider.value)")
sliderValue = Int(slider.value)
}
else if (Increment >= slider.minimumValue)
{
slider.setValue(newval, animated: true)
}
}