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

下面是我封装好的一个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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,695评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,589评论 2 45
  • RocketMQ作为一款分布式的消息中间件(阿里的说法是不遵循任何规范的,所以不能完全用JMS的那一套东西来看它)...
    光剑书架上的书阅读 2,340评论 0 6
  • “越努力越幸运” 这简单的六个字,不知道包含了多少人的梦。每当累了痛了苦了,笑着对自己说一句:“越努力越幸运!”这...
    Juliet_M阅读 359评论 0 0
  • #include Void main ( ) { ...
    湫兮如阅读 316评论 0 0