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:
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:
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!