Swift realizes example sharing of SMS Verification code countdown in IOS applications _ios

Source: Internet
Author: User

Before we begin, let's take a look at a concept attribute observer (property observers):

The property watcher monitors and responds to changes in property values, calling the property observer whenever a property is set, or even when the new value is the same as the current value.

You can add one or all of the following observers to a property:

    • Willset is called before the new value is set.
    • Didset is called immediately after the new value is set

Let's start with our tutorial and show you the final results:

First declare a send button:

Copy Code code as follows:

var sendbutton:uibutton!

To add a property to the Send button in the Viewdidload method:
Copy Code code as follows:

Override Func Viewdidload () {
Super.viewdidload ()

Sendbutton = UIButton ()
Sendbutton.frame = CGRect (x:40, y:100, width:view.bounds.width-80, height:40)
Sendbutton.backgroundcolor = Uicolor.redcolor ()
Sendbutton.settitlecolor (Uicolor.whitecolor (), forstate:. Normal)
Sendbutton.settitle ("Get Authentication Code", Forstate:.) Normal)
Sendbutton.addtarget (Self, Action: "Sendbuttonclick:", forControlEvents:. Touchupinside)

Self.view.addSubview (Sendbutton)
}


Next, declare a variable remainingseconds represents the number of seconds remaining for the current countdown:
Copy Code code as follows:

var remainingseconds = 0

We add a Willset method to Remainingseconds, which is invoked when the Remainingseconds value is about to change and passes the value to the parameter newvalue:
Copy Code code as follows:

var remainingseconds:int = 0 {
Willset {
Sendbutton.settitle ("Verify code has been sent (newvalue) seconds to retrieve)", Forstate:. Normal)

If NewValue <= 0 {
Sendbutton.settitle ("Re-acquire authentication code", Forstate:. Normal)
Iscounting = False
}
}
}


Updates the display text of Sendbutton when remainingseconds changes.

Countdown function We use Nstimer implementation, first declare a Nstimer instance:

Copy Code code as follows:

var countdowntimer:nstimer?

Then we declare a variable to turn the countdown on and off:
Copy Code code as follows:

var iscounting = false {
Willset {
If NewValue {
Countdowntimer = Nstimer.scheduledtimerwithtimeinterval (1, target:self, selector: "UpdateTime", Userinfo:nil, repeats : TRUE)

Remainingseconds = 10
Sendbutton.backgroundcolor = Uicolor.graycolor ()
} else {
Countdowntimer? Invalidate ()
Countdowntimer = Nil

Sendbutton.backgroundcolor = Uicolor.redcolor ()
}

sendbutton.enabled =!newvalue
}
}


Again, we add a Willset method to iscounting, and when Iscounting's newvalue is true, we call the Nstimer class method
ScheduledTimerWithTimeInterval:target:selector:userInfo:repeats: Create and launch the Countdowntimer instance that you just declared, This instance calls UpdateTime once every second: method:
Copy Code code as follows:

Func UpdateTime (Timer:nstimer) {
When the timer starts, the Remainingseconds value is reduced by seconds
Remainingseconds-= 1
}

When Iscounting's newvalue is false, we stop Countdowntimer and set Countdowntimer to nil.

In addition we set the countdown time (here for the demo time set to 5 seconds) and send the button in different iscounting state style (where the background color adjusted) and whether clickable.

Finally implement the Sendbuttonclick: method, which is called when the Sendbutton is clicked:

Copy Code code as follows:

Func Sendbuttonclick (Sender:uibutton) {
Start Countdown
Iscounting = True
}

Complete!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.