美文网首页
2.NSTimer的用法,开起,关闭,停止,继续

2.NSTimer的用法,开起,关闭,停止,继续

作者: noonez | 来源:发表于2016-03-25 11:38 被阅读643次

下面是我封装好的一个Timer,语言:Swift3.0

import UIKit

class GHTimer: NSObject {
    private var _timer:Timer!
    /**
     运行中的状态值
    **/
    private var _running = false
    public var running:Bool {
        get{
            return _running
        }
    }
    /**
     暂停或停止的状态值
    **/
    private var _isPause = true
    public var isPause:Bool{
        get{
            return _isPause
        }
    }
    /**
     执行的间隔时间(默认3秒)
     重新设置这个值之后需要重新启动timer
    **/
    public var intervalTime:Double = 3 {
        willSet{
            if intervalTime != newValue && _timer != nil {
                if _running {
                    close()
                }
                _timer = Timer(timeInterval: TimeInterval(newValue), repeats: repeats, block: run)
            }
        }
    }
    /**
     一直重复执行(true)or执行一次(false,默认)
     重新设置这个值之后需要重新启动timer
    **/
    public var repeats:Bool = false {
        willSet{
            if repeats != newValue && _timer != nil {
                if _running {
                    close()
                }
                _timer = Timer(timeInterval: TimeInterval(intervalTime), repeats: newValue, block: run)
            }
        }
    }
    /**
     需要重复执行的功能
    **/
    public var repeatRun:(()->Void)?
    
    init(interval:Double, repeats:Bool, repeatRun: @escaping (()->Void)) {
        self.intervalTime = interval
        self.repeats = repeats
        self.repeatRun = repeatRun
    }
    /**
     启动timer
    **/
    public func start() {
        if !_running {
            _timer = Timer(timeInterval: TimeInterval(intervalTime), repeats: repeats, block: run)
            RunLoop.current.add(_timer, forMode: .commonModes)
            _running = true
            _isPause = false
        }
    }
    /**
     关闭timer
    **/
    public func close() {
        if _running {
            _isPause = true
            _running = false
            _timer.invalidate()
            _timer = nil
        }
    }
    /**
     停止或暂停timer
    **/
    public func stop() {
        if _running && !_isPause {
            _timer.fireDate = Date.distantFuture
            _isPause = true
        }
    }
    /**
     从停止或暂停中恢复timer
    **/
    public func resume() {
        if _running && _isPause {
            _timer.fireDate = Date(timeIntervalSinceNow: intervalTime)
            _isPause = false
        }
    }
    private func run(timer:Timer) {
        repeatRun?()
    }
}

Example:

import UIKit

class ViewController: UIViewController {

    var timer:GHTimer!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let timeTimer = GHTimer(interval: 1, repeats: true, repeatRun: countTime)
        timeTimer.start()
        timer = GHTimer(interval: 2, repeats: true, repeatRun: run)
        timer.start()
    }
    var count = 1
    func countTime() {
        print(count)
        count = 1 + count % 5
    }
    func run() {
        print("repeat: ", timer.intervalTime)
    }
    @IBAction func onClose(_ sender: UIButton) {
        if timer.running{
            print("close")
            timer.close()
        }else{
            print("start")
            timer.start()
        }
    }
    @IBAction func onPause(_ sender: UIButton) {
        if !timer.isPause {
            print("pause")
            timer.stop()
        }else {
            print("resume")
            timer.resume()
        }
    }
    @IBAction func onChangeInterval(_ sender: UIButton) {
        print("change interval")
        timer.intervalTime = 5
        timer.start()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

源码实践:https://github.com/ghyh22/GHTimer

相关文章

  • 2.NSTimer的用法,开起,关闭,停止,继续

    下面是我封装好的一个Timer,语言:Swift3.0 Example: 源码实践:https://github....

  • iOS-后台任务相关

    研究一波吧. 1.iOS 后台任务2.NSTimer的基础用法以及程序挂起后NSTimer仍然可以在后台运行计时3...

  • 关闭窗口(window.close)

    close()关闭窗口 用法: window.close();//关闭本窗口 或 <窗口对象>.close();/...

  • 停止Logstash

    停止关闭检测 关闭正在运行的Logstash实例包括日下步骤: 停止所有的输入、过滤和输出插件 处理所有的正在被处...

  • systemd学习笔记

    systemctl 重启 systemctl reboot 关闭 systemctl poweroff CPU停止...

  • gitlab安装

    关闭firewall: systemctl stop firewalld.service #停止firewall ...

  • go channel的常规用法

    循环获取channel 如果需要停止使用channel,需要手动将channel关闭 关闭后的channel还能获...

  • 安装iptables防火墙

    关闭firewall: systemctl stop firewalld.service #停止firewalls...

  • 继续还是停止?

    最近的工作很忙,忙得觉得心烦,年底的工作纷繁复杂。也不知往年是怎么过的,今年就是特别烦。 以至...

  • Android ADB 命令整理

    . 点击电源键 开启飞行模式 关闭飞行模式 启用GPS 禁用GPS WIFI 打开 关闭 服务 开启 停止

网友评论

      本文标题:2.NSTimer的用法,开起,关闭,停止,继续

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