Swift定时器

Snip20171225_4.png
代码在这

// 定义需要计时的时间
        var timeCount = 60
        // 在global线程里创建一个时间源
        let codeTimer = DispatchSource.makeTimerSource(queue:DispatchQueue.global())
        // 设定这个时间源是每秒循环一次,立即开始
        codeTimer.scheduleRepeating(deadline: .now(), interval: .seconds(1))
        // 设定时间源的触发事件
        codeTimer.setEventHandler(handler: {
            // 每秒计时一次
            timeCount = timeCount - 1
            
           
            // 时间到了取消时间源
            if timeCount <= 0 {
                
                codeTimer.cancel()
            }
            // 返回主线程处理一些事件,更新UI等等
            DispatchQueue.main.async {
                sender.setTitle("\(timeCount)s", for: UIControlState.normal)
            }
        })
        // 启动时间源
        codeTimer.resume()
    }
        
简单使用
Untit.gif
      override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(composeBtn)
        composeBtn.frame = CGRect(x: 100, y: 100, width: 150, height: 40)
    }
    
    
    fileprivate lazy var composeBtn:UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor.red
        btn.setTitle("获取验证码", for: UIControlState.normal)
        btn.addTarget(self, action: #selector(btnClick(sender:)), for: UIControlEvents.touchUpInside)
        return btn
    }()
    
    func btnClick(sender:UIButton) {
        // Do any additional setup after loading the view, typically from a nib.
        // 定义需要计时的时间
        var timeCount = 60
        // 在global线程里创建一个时间源
        let codeTimer = DispatchSource.makeTimerSource(queue:DispatchQueue.global())
        // 设定这个时间源是每秒循环一次,立即开始
        codeTimer.scheduleRepeating(deadline: .now(), interval: .seconds(1))
        // 设定时间源的触发事件
        codeTimer.setEventHandler(handler: {
            // 每秒计时一次
            timeCount = timeCount - 1
            
            // 返回主线程处理一些事件,更新UI等等
            DispatchQueue.main.async {
                sender.isEnabled = false
                sender.backgroundColor = UIColor.lightGray
                sender.setTitle("\(timeCount)s", for: UIControlState.normal)
            }
            // 时间到了取消时间源
            if timeCount <= 0 {
                DispatchQueue.main.async {
                    sender.isEnabled = true
                    sender.backgroundColor = UIColor.red
                    sender.setTitle("重新获取验证码", for: UIControlState.normal)
                }
                codeTimer.cancel()
            }
         
        })
        // 启动时间源
        codeTimer.resume()
    }

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 开发中不可避免会用到注册获取验证码,在验证码定时器使用方面别出心裁,各有妙招,今天推荐一个比较实用的定时器使用方法...
    Kean_Qi阅读 479评论 0 0
  • 1.定义全局 var timer = NSTimer?() var count = 10 var btn = UI...
    小_蜡笔阅读 1,224评论 0 0
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,034评论 25 709
  • 想说都是杂言 粗略看到网传演员QRL突然死亡 网传 以为绝对是谣传 没想到点进去后已经确认是事实 印象中此人该是阳...
    大风小雨阅读 315评论 0 0
  • Suppose an array sorted in ascending order is rotated at ...
    PeterHe888阅读 289评论 0 0

友情链接更多精彩内容