美文网首页
swift 验证码倒计时代码

swift 验证码倒计时代码

作者: JackMayx | 来源:发表于2016-06-03 15:16 被阅读780次

    直接上代码

    import UIKit
    
    ///验证码倒计时
    class RFCodeCountdownBtn:UIButton{
        ///验证码倒计时的起始秒数
        var maxTimer = 10
        
        var countDown = false{
            didSet{
                
                if oldValue != countDown{
                    countDown ? startCountDown() : stopCountDown()
                }
            }
        }
        
        private var second = 0
        private var timer: NSTimer?
        ///倒计时显示
        private var timeLabel: UILabel!
        private var normalText: String!
        private var normalTextColor: UIColor!
        private var disabledText: String!
        private var disabledTextColor: UIColor!
        
        
        override func awakeFromNib() {
            super.awakeFromNib()
            setuptimeLabel()
        }
        
        deinit {
            countDown = false
        }
        
        // MARK: Setups
        private func setuptimeLabel() {
            normalText = titleForState(.Normal)!
            disabledText = titleForState(.Disabled)!
            normalTextColor = UIColor .redColor()
            disabledTextColor = UIColor .greenColor()
            setTitle("", forState: .Normal)
            setTitle("", forState: .Disabled)
            timeLabel = UILabel(frame:bounds)
            timeLabel.textAlignment = .Center
            timeLabel.font = titleLabel?.font
            timeLabel.textColor = normalTextColor
            timeLabel.text = normalText
            addSubview(timeLabel)
        }
        
        // MARK: Private
        private func startCountDown() {
            second = maxTimer
            updateDisabled()
            
            if timer != nil {
                timer!.invalidate()
                timer = nil
            }
            
            timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(RFCodeCountdownBtn.updateCountdown), userInfo: nil, repeats: true)
        }
        
        private func stopCountDown() {
            timer?.invalidate()
            timer = nil
            updateNormal()
        }
        
        private func updateNormal() {
            enabled = true
            timeLabel.textColor = normalTextColor
            timeLabel.text = normalText
        }
        
        private func updateDisabled() {
            enabled = false
            timeLabel.textColor = disabledTextColor
            timeLabel.text = "重新发送" + String(second) + "'"
        }
        
        @objc private func updateCountdown() {
            //        print("second == \(second)")
            if second <= 1 {
                countDown = false
            } else {
                second = second - 1
                updateDisabled()
            }
        }
    }
    

    用法:

    var btn: RFCodeCountdownBtn!
    
    ///验证码按钮实现方法
        @IBAction func getCodeMethod(sender: AnyObject) {
            
            btn.countDown = true
            btn.maxTimer = 10
            
            
        }
    

    相关文章

      网友评论

          本文标题:swift 验证码倒计时代码

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