Swift4使用GCD實(shí)現(xiàn)計(jì)時(shí)器
開(kāi)發(fā)過(guò)程中,我們可能會(huì)經(jīng)常使用到計(jì)時(shí)器。蘋(píng)果為我們提供了Timer。但是在平時(shí)使用過(guò)程中會(huì)發(fā)現(xiàn)使用Timer會(huì)有許多的不便
1:必須保證在一個(gè)活躍的runloop,我們知道主線程的runloop是活躍的,但是在其他異步線程runloop就需要我們自己去開(kāi)啟,非常麻煩。
2:Timer的創(chuàng)建和銷毀必須在同一個(gè)線程??缇€程就操作不了
3:內(nèi)存問(wèn)題。可能循環(huán)引用造成內(nèi)存泄露
由于存在上述問(wèn)題,我們可以采用GCD封裝來(lái)解決。
import UIKit typealias ActionBlock = () -> () class MRGCDTimer: NSObject { static let share = MRGCDTimer() lazy var timerContainer = [String : DispatchSourceTimer]() /// 創(chuàng)建一個(gè)名字為name的定時(shí) /// /// - Parameters: /// - name: 定時(shí)器的名字 /// - timeInterval: 時(shí)間間隔 /// - queue: 線程 /// - repeats: 是否重復(fù) /// - action: 執(zhí)行的操作 func scheduledDispatchTimer(withName name:String?, timeInterval:Double, queue:DispatchQueue, repeats:Bool, action:@escaping ActionBlock ) { if name == nil { return } var timer = timerContainer[name!] if timer==nil { timer = DispatchSource.makeTimerSource(flags: [], queue: queue) timer?.resume() timerContainer[name!] = timer } timer?.schedule(deadline: .now(), repeating: timeInterval, leeway: .milliseconds(100)) timer?.setEventHandler(handler: { [weak self] in action() if repeats==false { self?.destoryTimer(withName: name!) } }) } /// 銷毀名字為name的計(jì)時(shí)器 /// /// - Parameter name: 計(jì)時(shí)器的名字 func destoryTimer(withName name:String?) { let timer = timerContainer[name!] if timer == nil { return } timerContainer.removeValue(forKey: name!) timer?.cancel() } /// 檢測(cè)是否已經(jīng)存在名字為name的計(jì)時(shí)器 /// /// - Parameter name: 計(jì)時(shí)器的名字 /// - Returns: 返回bool值 func isExistTimer(withName name:String?) -> Bool { if timerContainer[name!] != nil { return true } return false } }
使用方法
MRGCDTimer.share.scheduledDispatchTimer(withName: "name", timeInterval: 1, queue: .main, repeats: true) { //code self.updateCounter() }
取消計(jì)時(shí)器
MRGCDTimer.share.destoryTimer(withName: "name")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺析Swift中struct與class的區(qū)別(匯編角度底層分析)
這篇文章主要介紹了Swift中struct與class的區(qū)別 ,本文從匯編角度分析struct與class的區(qū)別,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Swift?中的?RegexBuilder學(xué)習(xí)指南
這篇文章主要為大家介紹了Swift中的RegexBuilder學(xué)習(xí)指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04swift實(shí)現(xiàn)顏色漸變以及轉(zhuǎn)換動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了swift實(shí)現(xiàn)顏色漸變以及轉(zhuǎn)換動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01swift 3.0 正則表達(dá)式查找/替換字符的實(shí)現(xiàn)代碼
正則表達(dá)式使用單個(gè)字符串來(lái)描述、匹配一系列符合某個(gè)句法規(guī)則的字符串。本文重點(diǎn)給大家介紹swift 3.0 正則表達(dá)式查找/替換字符的實(shí)現(xiàn)代碼,需要的朋友參考下吧2017-08-08簡(jiǎn)陋的swift carthage copy-frameworks 輔助腳本代碼
下面小編就為大家分享一篇簡(jiǎn)陋的swift carthage copy-frameworks 輔助腳本代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01使用Swift實(shí)現(xiàn)iOS App中解析XML格式數(shù)據(jù)的教程
這篇文章主要介紹了使用Swift實(shí)現(xiàn)iOS App中解析XML格式數(shù)據(jù)的教程,講到了iOS中提供的NSXMLParser和NSXMLParserDelegate兩個(gè)API的用法,需要的朋友可以參考下2016-04-04