美文网首页
Swift定时器

Swift定时器

作者: 轻描淡写swift | 来源:发表于2017-12-25 14:39 被阅读52次
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()
    }

相关文章

  • 80-Swift之CADisplayLink的解说

    引言 在App的开发中定时器是常用的组件。我们在55 - Swift 之 Timer (NSTimer )定时器已...

  • RxSwift中的Timer

    RxSwift中的Timer 我们在项目中经常会用到定时器,先来看下swift中使用定时器的几种方式: Timer...

  • swift GCD-定时器(DispatchSourceTime

    前言: 了解swift GCD 与OC GCD请看我的其他总结文章,这篇文章只是对swift GCD定时器的一个实...

  • Swift3.0之后GCD定时器如何创建

    Swift3.0很多语法都有变化,GCD的定时器也发生了变化 1.首先,创建定时器,和之前的有所不同,代码如下: ...

  • Swift定时器

    代码在这 简单使用

  • swift 定时器

    定时器的两种简单实现方式: 1.timer 2.GCD 上面两种方法以及实现简单的定时器,但是还有一个隐藏的问题,...

  • Swift定时器

    1.定义全局 var timer = NSTimer?() var count = 10 var btn = UI...

  • 定时器 swift

  • swift定时器

    全局可以一直存在一个主定时器 每个cell单独控制自己的time值(一个数组去控制) 因为每个app都可能存在回到...

  • swift GDC定时器

网友评论

      本文标题:Swift定时器

      本文链接:https://www.haomeiwen.com/subject/aojlgxtx.html