您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)IOS小組件如何實(shí)現(xiàn)時(shí)鐘按秒刷新功能,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
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)前日期和時(shí)間與指定日期之間的差值(絕對(duì)值),而不管該日期是將來的還是過去的日期。使用offset樣式顯示當(dāng)前日期和時(shí)間與指定日期之間的時(shí)差,表示將來的日期帶有減號(hào)(-)前綴,而過去的日期帶有加號(hào)(+)前綴。
let components = DateComponents(minute: 15) let futureDate = Calendar.current.date(byAdding: components, to: Date())! Text(futureDate, style: .timer) // Displays: // 15:00
??對(duì)于將來的日期,timer樣式將遞減計(jì)數(shù)(倒計(jì)時(shí)),直到當(dāng)前時(shí)間達(dá)到指定的日期和時(shí)間為止,并在日期經(jīng)過時(shí)遞增計(jì)數(shù)(計(jì)時(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
??使用 style: .time樣式,如果當(dāng)前的時(shí)間比指定的時(shí)間大,則時(shí)間就會(huì)累計(jì)?;谶@個(gè)原理,我們只需要把時(shí)間起點(diǎn)定在每天的0點(diǎn)即可,根據(jù)當(dāng)前的時(shí)間計(jì)算出今天的開始時(shí)間。以下方法可以根據(jù)12,24小時(shí)制度,獲取當(dāng)天起點(diǎn)時(shí)間。
//獲取當(dāng)天開始的日期,給Date增加一個(gè)拓展方法 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)! } } // 實(shí)現(xiàn)一天內(nèi)的計(jì)時(shí)器 Text(Date().getCurrentDayStart(true), style: .timer)
關(guān)于“IOS小組件如何實(shí)現(xiàn)時(shí)鐘按秒刷新功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。