美文网首页
倒计时组件:xz_CountDownButton - iOS 组

倒计时组件:xz_CountDownButton - iOS 组

作者: z_er | 来源:发表于2018-09-22 22:53 被阅读0次

    首先定义一些常量

    fileprivate let second_args = "%ld"
    fileprivate let default_max_count = 5
    fileprivate let default_send_tip = "验证码"
    fileprivate let default_wait_tip = "\(second_args)秒"
    fileprivate let trigger_per_rate: Int = 1
    

    定义倒计时按钮状态enum

    enum CCountDownStatus {
        case send       //待发送状态
        case wait       //已发送状态
    }
    

    属性

    ///最大计数时间
        public var maxCount = default_max_count {
            didSet {
                self.count = maxCount
            }
        }
        
        ///默认提示语句
        public var sendTip = default_send_tip {
            didSet {
                self.setTitle(self.sendTip, for: .normal)
            }
        }
        
        ///发送完成后提示语句 tip: -- 秒数以%ld替代   eg: 请等待:%ld秒
        public var waitTip = default_wait_tip
        
        /// timer
        private var timer: Timer?
        
        ///用于计数
        private var count = default_max_count
        
        ///发送状态
        public var status: CCountDownStatus = .send {
            didSet {
                if oldValue == .send && status == .wait {
                    self.statusDidChanged()
                }
            }
        }
    

    实现方法

    init() {
            super.init(frame: .zero)
            self.setTitle(self.sendTip, for: .normal)
        }
        
        convenience init(_ maxCount: Int?, sendTip: String?, waitTip: String?) {
            self.init()
            if let max_count = maxCount {
                self.maxCount = max_count
            }
            if let send_tip = sendTip {
                self.sendTip = send_tip
            }
            if let wait_tip = waitTip {
                self.waitTip = wait_tip
            }
            self.setTitle(self.sendTip, for: .normal)
        }
        
        deinit {
            self.timer?.invalidate()
            self.timer = nil
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        private func statusDidChanged() -> Void {
            if self.isSelected { return }
            self.status = .wait
            timer = Timer.scheduledTimer(withTimeInterval: Double(trigger_per_rate), repeats: true, block: {[weak self] (timer) in
                if self!.count <= 0 {
                    self?.timer?.invalidate()
                    self?.timer = nil
                    self?.count = self!.maxCount
                    self?.isSelected = false
                    self?.status = .send
                    self?.setTitle(self?.sendTip, for: .normal)
                    self?.setTitle(String.init(format: self!.waitTip, self!.count), for: .selected)
                } else {
                    self?.isSelected = true
                    self?.setTitle(String.init(format: self!.waitTip, self!.count), for: .selected)
                    self?.count -= trigger_per_rate
                }
            })
        }
    

    CCoundDownButton 使用

            var c = CCountDownButton.init(nil, sendTip: nil, waitTip: nil)
            
    //        c = CCountDownButton.init()
    //        c.sendTip = "点击发送"
    //        c.waitTip = "%ld秒后"
    //        c.maxCount = 100
            
            c.backgroundColor = UIColor.blue
            self.view.addSubview(c)
            c.addTarget(self, action: #selector(didTouchCoundDown(_:)), for: .touchUpInside)
            c.frame = CGRect.init(x: 0, y: 0, width: 100, height: 100)
            c.center = self.view.center
        }
        
        @objc private func didTouchCoundDown(_ sender: CCountDownButton) -> Void {
            //再向服务器请求数据成功后修改状态即可
            sender.status = .wait        
        }
    

    pod安装

    pod 'xz_CountDownButton', '~> 0.0.1'
    

    相关文章

      网友评论

          本文标题:倒计时组件:xz_CountDownButton - iOS 组

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