快速点击按钮启动计时器 [英] Starting timer on button click in swift

查看:45
本文介绍了快速点击按钮启动计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我快速创建了一个计时器,以便在按下按钮时一次又一次地将 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)
        }
    }

这篇关于快速点击按钮启动计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆