[英]UIKit Swift 5: How to update UI every new minute
我正在嘗試更新我的 UI 上的倒數計時器,它以分鍾為單位倒計時,所以我需要在每一個新的分鍾開始時更新它,這與每分鍾更新一次不同。 我當前的解決方案是初始化一個計時器,該計時器每十分之一秒調用一次updateUI()
function,這遠非理想:
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector:#selector(TimesViewController.updateUI), userInfo: nil, repeats: true)
有沒有辦法在新的一分鍾開始時調用 function ?
創建每秒檢查系統日期的計時器。 例如;
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (_) in
let second = Calendar.current.component(.second, from: Date())
if second == 0 {
// new minute
updateUI()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.