GithubHelp home page GithubHelp logo

gpx-nslog / wmcountdown Goto Github PK

View Code? Open in Web Editor NEW

This project forked from realwaynezhang/wmcountdown

0.0 1.0 0.0 2.23 MB

一百行代码搞定商城活动倒计时,采用Swift4.2语法,倒计时格式为天,时,分,秒。

License: MIT License

Swift 100.00%

wmcountdown's Introduction

WMCountDown

一百行代码封装商城活动倒计时,用Swift4.2,倒计时格式为天,时,分,秒。

展示

优点:

采用DispatchTimer定时器,在globalQueue中执行定时器,只在当前控制器开始倒计时,离开立即停止。

用法:

将WMCountDown.swift文件拖拽到项目中。 ViewController使用方法如下:

  /// 懒加载 倒计时
    lazy var countdownTimer: WMCountDown = {
        let timer = WMCountDown()
        // 此闭包可以在本类任意方法中写
        timer.countDown = { [weak self] (d, h, m, s) in
            self?.dayLabel.text = "\(d)天:"
            self?.hourLabel.text = "\(h)时:"
            self?.minuteLabel.text = "\(m)分:"
            self?.secondLabel.text = "\(s)"
        }
        return timer
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        countdownTimer.resume() // 恢复倒计时
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        countdownTimer.suspend() // 停止倒计时
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 开始倒计时
        // 可以传递开始时间参数,用于计算倒计时时间差,不传默认从系统当前时间开始计算时间差
        // countdownTimer.start(with: "2018-12-17 22:49:00", end: "2018-12-19 22:49:00")
        countdownTimer.start(with: nil, end: "2019-12-19 22:49:00")
    }
    
    
    deinit {
        /// 可写可不写
        countdownTimer.stop()
    }

TableViewCell 使用方法:

class CountDownCell:UITableViewCell{

     let countDownTimer =WMCountDown()

    override func awakeFromNib() {

        super.awakeFromNib()

        countDownTimer.countDown= { [weak self ] (d, h, m, s) in

            let time = d +":"+ h +":"+ m +":"+ s

            self?.textLabel?.text= time

            print("时间是:\(d):\(h):\(m):\(s)")

        }

        // 开始倒计时

        // 可以传递开始时间参数,用于计算倒计时时间差,不传,默认从系统当前时间开始计算时间差

        // countdownTimer.start(with: "2018-12-17 22:49:00", end: "2018-12-19 22:49:00")*

        let minute =randomInRange(range: (1..<60))

        let second =randomInRange(range: (1..<60))

        countDownTimer.start(with:**nil**, end:"2019-12-19 22:\(minute):\(second)")

    }

    deinit  {

        countDownTimer.stop()

    }

}

如果对您有帮助,请给个Star!😄,Thank you for look!

wmcountdown's People

Contributors

realwaynezhang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.