IOS小組件實現(xiàn)時鐘按秒刷新功能
引言
上一節(jié)中我們了解了IOS小組件的刷新機(jī)制,發(fā)現(xiàn)根本沒法實現(xiàn)按秒刷新,但是看別的App里面有做到,以為用了什么黑科技,原來是因為系統(tǒng)提供了一個額外的機(jī)制實現(xiàn)時間的動態(tài)更新,不用走小組件的刷新機(jī)制。
Text控件支持顯示日期時間,下面是來自官網(wǎng)的代碼
計算時間差
let components = DateComponents(minute: 11, second: 14) let futureDate = Calendar.current.date(byAdding: components, to: Date())! Text(futureDate, style: .relative) // Displays: // 11 min, 14 sec Text(futureDate, style: .offset) // Displays: // -11 minutes
使用relative樣式可以顯示當(dāng)前日期和時間與指定日期之間的差值(絕對值),而不管該日期是將來的還是過去的日期。使用offset樣式顯示當(dāng)前日期和時間與指定日期之間的時差,表示將來的日期帶有減號(-)前綴,而過去的日期帶有加號(+)前綴。
倒計時和計時器
let components = DateComponents(minute: 15) let futureDate = Calendar.current.date(byAdding: components, to: Date())! Text(futureDate, style: .timer) // Displays: // 15:00
對于將來的日期,timer樣式將遞減計數(shù)(倒計時),直到當(dāng)前時間達(dá)到指定的日期和時間為止,并在日期經(jīng)過時遞增計數(shù)(計時器)。
顯示絕對日期或時間
// Absolute Date or Time let components = DateComponents(year: 2020, month: 4, day: 1, hour: 9, minute: 41) let aprilFirstDate = Calendar.current(components)! Text(aprilFirstDate, style: .date) Text("Date: \(aprilFirstDate, style: .date)") Text("Time: \(aprilFirstDate, style: .time)") // Displays: // April 1, 2020 // Date: April 1, 2020 // Time: 9:41AM
顯示兩個日期之間的時間間隔
let startComponents = DateComponents(hour: 9, minute: 30) let startDate = Calendar.current.date(from: startComponents)! let endComponents = DateComponents(hour: 14, minute: 45) let endDate = Calendar.current.date(from: endComponents)! Text(startDate ... endDate) Text("The meeting will take place: \(startDate ... endDate)") // Displays: // 9:30AM-2:45PM // The meeting will take place: 9:30AM-2:45PM
實現(xiàn)一天時間的計時器
使用 style: .time樣式,如果當(dāng)前的時間比指定的時間大,則時間就會累計?;谶@個原理,我們只需要把時間起點定在每天的0點即可,根據(jù)當(dāng)前的時間計算出今天的開始時間。以下方法可以根據(jù)12,24小時制度,獲取當(dāng)天起點時間。
//獲取當(dāng)天開始的日期,給Date增加一個拓展方法 extension Date { func getCurrentDayStart(_ isDayOf24Hours: Bool)-> Date { let calendar:Calendar = Calendar.current; let year = calendar.component(.year, from: self); let month = calendar.component(.month, from: self); let day = calendar.component(.day, from: self); let components = DateComponents(year: year, month: month, day: day, hour: 0, minute: 0, second: 0) return Calendar.current.date(from: components)! } } // 實現(xiàn)一天內(nèi)的計時器 Text(Date().getCurrentDayStart(true), style: .timer)
結(jié)語
通過IOS Text控件我們實現(xiàn)了按秒刷新的計時器,所以數(shù)字時鐘的按秒刷新算是解決了,但是怎么實現(xiàn)表盤時鐘的秒針360度旋轉(zhuǎn)呢?如果讓秒針精確的對應(yīng)當(dāng)前的時間,應(yīng)該做不到了。后面再繼續(xù)研究,如果有解決方案的可以貢獻(xiàn)一下,感謝。
相關(guān)文章
iOS如何固定UITableView中cell.imageView.image的圖片大小
這篇文章主要給大家介紹了關(guān)于iOS如何固定UITableView中cell.imageView.image圖片大小的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11iOS中詳解Block作為property屬性實現(xiàn)頁面之間傳值
這篇文章主要介紹了iOS中Block作為property屬性實現(xiàn)頁面之間傳值的相關(guān)知識點,以及代碼分享,一起學(xué)習(xí)下吧。2018-02-02ios scrollview嵌套tableview同向滑動的示例
本篇文章主要介紹了ios scrollview嵌套tableview同向滑動的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11iOS實時監(jiān)控網(wǎng)絡(luò)狀態(tài)的改變
這篇文章主要為大家詳細(xì)介紹了iOS實時監(jiān)控網(wǎng)絡(luò)狀態(tài)的改變的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-08-08