最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]swift去实现一个发送验证码按钮且支持点击后自动倒计时

iOS crifan 835浏览 0评论

swift中,之前都是通过额外的设置去实现点击发送验证码按钮后的倒计时。

现在去看看是否方便扩展UIButton

去实现,点击后,就自动倒计时

然后可以设置停止倒计时

的按钮。

最后,自己实现了对应的代码:

<code>/***************************************************************************
 * CountdownButton.swift
 ***************************************************************************/

let CountdownButtonAutoStartCounddown:Bool = true
let CountdownButtonNormalTitle:String = "发送验证码"
let CountdownButtonTotalNum:Int = 60
let CountdownButtonDisabledTitleColor:UIColor = UIColor.grayColor()
//let CountdownButtonDisabledTitleFormat:String = "发送验证码(%d秒)"
let CountdownButtonDisabledTitleFormat:String = "重新发送(%d秒)"


class CountdownButton: UIButton {
    //class CountdownButton: CommonButton {
    var autoStartCounddown:Bool
    var normalTitle:String
    var totalCountdownNum:Int
    var disabledTitleColor:UIColor
    var disabledTitleFormat:String

    var countdownTimer:NSTimer
    var countdownCurNum:Int

    override init(frame: CGRect) {
        self.autoStartCounddown = CountdownButtonAutoStartCounddown
        self.normalTitle = CountdownButtonNormalTitle
        self.totalCountdownNum = CountdownButtonTotalNum
        self.disabledTitleColor = CountdownButtonDisabledTitleColor
        self.disabledTitleFormat = CountdownButtonDisabledTitleFormat

        self.countdownCurNum = 0
        self.countdownTimer = NSTimer()

        super.init(frame: frame)

        if self.autoStartCounddown {
            self.addTarget(self, action: #selector(self.startCountdown), forControlEvents: UIControlEvents.TouchUpInside)
        }

        self.setTitle(self.normalTitle, forState:UIControlState.Normal)
    }

    convenience init(normalTitle:String, autoStartCounddown:Bool = CountdownButtonAutoStartCounddown, totalCountdownNum:Int = CountdownButtonTotalNum, countingDownTitleColor:UIColor = CountdownButtonDisabledTitleColor,countingDownTitleFormat:String = CountdownButtonDisabledTitleFormat) {
        self.init(frame: CGRectZero)

        self.autoStartCounddown = autoStartCounddown
        if !self.autoStartCounddown {
            self.removeTarget(self, action: #selector(self.startCountdown), forControlEvents: UIControlEvents.TouchUpInside)
        }

        self.normalTitle = normalTitle
        self.setTitle(self.normalTitle, forState:UIControlState.Normal)

        self.totalCountdownNum = totalCountdownNum

        self.disabledTitleColor = countingDownTitleColor
        self.setTitleColor(self.disabledTitleColor, forState: UIControlState.Disabled)

        self.disabledTitleFormat = countingDownTitleFormat
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func startCountdown() {
        gLog.debug("")

        self.countdownCurNum = self.totalCountdownNum

        updateCountdownLabel()

        self.countdownTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector:#selector(self.updateCountdown), userInfo: nil, repeats: true)
        NSRunLoop.currentRunLoop().addTimer(self.countdownTimer, forMode: NSRunLoopCommonModes)
    }

    func updateCountdown() {
        gLog.debug("self.countdownCurNum=\(self.countdownCurNum)")
        self.countdownCurNum -= 1

        if self.countdownCurNum &lt;= 0 {
            self.countdownTimer.invalidate()
            self.countdownCurNum  = 0
        }

        updateCountdownLabel()
    }

    func updateCountdownLabel(){
        gLog.debug("self.countdownCurNum=\(self.countdownCurNum)")

        dispatchMain_async({
            if self.countdownCurNum == 0 {
                self.setTitle(self.normalTitle, forState:UIControlState.Normal)
                self.enabled = true
            }else if self.countdownCurNum &gt; 0 {
                self.setTitle(String(format: self.disabledTitleFormat, self.countdownCurNum), forState:UIControlState.Disabled)
                self.enabled = false
            }

            //            self.updateSmsCodeButtonUI()
        })
    }

    func stopCountdown(){
        gLog.debug("stopCountdown: self.countdownCurNum=\(self.countdownCurNum)")

        self.countdownTimer.invalidate()

        self.countdownCurNum = 0

        updateCountdownLabel()
    }

    //    func updateSmsCodeButtonUI(){
    //        if self.enabled {
    //            self.backgroundColor = UIColor.whiteColor()
    //            self.layer.borderColor = ColorButtonBackgroud.CGColor
    //            self.setTitleColor(ColorButtonBackgroud, forState: UIControlState.Normal)
    //        } else {
    //            self.backgroundColor = UIColor.clearColor()
    //            self.layer.borderColor = ColorTextFieldBorderGray.CGColor
    //            self.setTitleColor(ColorTextFieldPlaceholderGray, forState: UIControlState.Disabled)
    //        }
    //    }
}


调用:
    /***************************************************************************
     * CountdownButton demo
     ***************************************************************************/

    //let getSmsCodeButton:CountdownButton = CountdownButton(normalTitle: "发送验证码", autoStartCounddown: false)
    let getSmsCodeButton:CountdownButton = CountdownButton(normalTitle: "发送验证码")
    print("getSmsCodeButton=\(getSmsCodeButton)")
//    self.addSubview(self.getSmsCodeButton)

    //if autoStartCounddown = true, then not need this
    getSmsCodeButton.startCountdown()

    getSmsCodeButton.stopCountdown()
</code>

最新最全代码详见:

github中的源码:

https://github.com/crifan/crifanLib/blob/master/swift/UI/Button/CountdownButton/CountdownButton.swift

和:

https://github.com/crifan/crifanLib/blob/master/swift/UI/Button/CountdownButton/CountdownButtonDemo.swift

转载请注明:在路上 » [已解决]swift去实现一个发送验证码按钮且支持点击后自动倒计时

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.229 seconds, using 18.81MB memory