Swift實(shí)現(xiàn)iOS應(yīng)用中短信驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)例分享
在開(kāi)始之前,我們先來(lái)了解一個(gè)概念 屬性觀測(cè)器(Property Observers):
屬性觀察器監(jiān)控和響應(yīng)屬性值的變化,每次屬性被設(shè)置值的時(shí)候都會(huì)調(diào)用屬性觀察器,甚至新的值和現(xiàn)在的值相同的時(shí)候也不例外。
可以為屬性添加如下的一個(gè)或全部觀察器:
- willSet在新的值被設(shè)置之前調(diào)用
- didSet在新的值被設(shè)置之后立即調(diào)用
接下來(lái)開(kāi)始我們的教程,先展示一下最終效果:
首先聲明一個(gè)發(fā)送按鈕:
var sendButton: UIButton!
在viewDidLoad方法中給發(fā)送按鈕添加屬性:
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("獲取驗(yàn)證碼", forState: .Normal)
sendButton.addTarget(self, action: "sendButtonClick:", forControlEvents: .TouchUpInside)
self.view.addSubview(sendButton)
}
接下來(lái)聲明一個(gè)變量remainingSeconds代表當(dāng)前倒計(jì)時(shí)剩余的秒數(shù):
var remainingSeconds = 0
我們給remainingSeconds添加一個(gè)willSet方法,這個(gè)方法會(huì)在remainingSeconds的值將要變化的時(shí)候調(diào)用,并把值傳遞給參數(shù)newValue:
var remainingSeconds: Int = 0 {
willSet {
sendButton.setTitle("驗(yàn)證碼已發(fā)送(\(newValue)秒后重新獲取)", forState: .Normal)
if newValue <= 0 {
sendButton.setTitle("重新獲取驗(yàn)證碼", forState: .Normal)
isCounting = false
}
}
}
當(dāng)remainingSeconds變化時(shí)更新sendButton的顯示文本。
倒計(jì)時(shí)的功能我們用NSTimer實(shí)現(xiàn),先聲明一個(gè)NSTimer實(shí)例:
var countdownTimer: NSTimer?
然后我們聲明一個(gè)變量來(lái)開(kāi)啟和關(guān)閉倒計(jì)時(shí):
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
}
}
同樣,我們給isCounting添加一個(gè)willSet方法,當(dāng)isCounting的newValue為true時(shí),我們通過(guò)調(diào)用NSTimer的類(lèi)方法
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:創(chuàng)建并啟動(dòng)剛才聲明的countdownTimer實(shí)例,這個(gè)實(shí)例每一秒鐘調(diào)用一次updateTime:方法:
func updateTime(timer: NSTimer) {
// 計(jì)時(shí)開(kāi)始時(shí),逐秒減少remainingSeconds的值
remainingSeconds -= 1
}
當(dāng)isCounting的newValue為false時(shí),我們停止countdownTimer并將countdownTimer設(shè)置為nil。
此外我們還設(shè)置了倒計(jì)時(shí)的時(shí)間(這里為了演示時(shí)間設(shè)置為5秒)和發(fā)送按鈕在不同isCounting狀態(tài)下的樣式(這里調(diào)整了背景色)和是否可點(diǎn)擊。
最后實(shí)現(xiàn)sendButtonClick:方法,這個(gè)方法在點(diǎn)擊sendButton時(shí)調(diào)用:
func sendButtonClick(sender: UIButton) {
// 啟動(dòng)倒計(jì)時(shí)
isCounting = true
}
完成!
相關(guān)文章
iOS schem與Universal Link 調(diào)試時(shí)踩坑解決記錄
這篇文章主要為大家介紹了iOS schem與Universal Link 調(diào)試時(shí)踩坑解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01iOS開(kāi)發(fā)中ViewController的頁(yè)面跳轉(zhuǎn)和彈出模態(tài)
這篇文章主要介紹了iOS開(kāi)發(fā)中ViewController的頁(yè)面跳轉(zhuǎn)和彈出模態(tài),ViewController是MVC開(kāi)發(fā)模式中一個(gè)重要的類(lèi),需要的朋友可以參考下2015-10-10iOS實(shí)現(xiàn)列表與網(wǎng)格兩種視圖的相互切換
相信大家應(yīng)該也都發(fā)現(xiàn)了,在現(xiàn)在很多的電商app中,都會(huì)有列表視圖和網(wǎng)格兩種視圖的相互切換。例如京東和淘寶。這樣更利于提高用戶的體驗(yàn)度,所以這篇文章小編就是大家分享下利用iOS實(shí)現(xiàn)列表與網(wǎng)格兩種視圖相互切換的方法,文中介紹的很詳細(xì),感興趣的下面來(lái)一起看看吧。2016-10-10IOS json 解析遇到錯(cuò)誤問(wèn)題解決辦法
這篇文章主要介紹了iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.解決辦法,需要的朋友可以參考下2017-01-01iOS swift實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的方法示例
在平時(shí)的iOS開(kāi)發(fā)中,我們進(jìn)行界面跳轉(zhuǎn)時(shí)一般都是采用系統(tǒng)默認(rèn)的轉(zhuǎn)場(chǎng)動(dòng)畫(huà),而下面這篇文章主要給大家介紹了關(guān)于iOS利用swift實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07