欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IOS小組件實(shí)現(xiàn)時(shí)鐘按秒刷新功能

 更新時(shí)間:2021年05月21日 15:44:48   作者:popfisher  
小組件運(yùn)行在單獨(dú)的進(jìn)程,如果異常會(huì)導(dǎo)致小組件進(jìn)程卡死了,一個(gè)小組件出問(wèn)題,其他小組件都不刷新了。既然刷新這么難控制,怎么實(shí)現(xiàn)數(shù)字時(shí)鐘按秒刷新呢?接下來(lái)通過(guò)代碼給大家介紹下ios小組件刷新功能的實(shí)現(xiàn),一起看看吧

引言

  上一節(jié)中我們了解了IOS小組件的刷新機(jī)制,發(fā)現(xiàn)根本沒(méi)法實(shí)現(xiàn)按秒刷新,但是看別的App里面有做到,以為用了什么黑科技,原來(lái)是因?yàn)橄到y(tǒng)提供了一個(gè)額外的機(jī)制實(shí)現(xiàn)時(shí)間的動(dòng)態(tài)更新,不用走小組件的刷新機(jī)制。

Text控件支持顯示日期時(shí)間,下面是來(lái)自官網(wǎng)的代碼

計(jì)算時(shí)間差

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ì)值),而不管該日期是將來(lái)的還是過(guò)去的日期。使用offset樣式顯示當(dāng)前日期和時(shí)間與指定日期之間的時(shí)差,表示將來(lái)的日期帶有減號(hào)(-)前綴,而過(guò)去的日期帶有加號(hào)(+)前綴。

倒計(jì)時(shí)和計(jì)時(shí)器

let components = DateComponents(minute: 15)
let futureDate = Calendar.current.date(byAdding: components, to: Date())!

Text(futureDate, style: .timer)
// Displays:
// 15:00

  對(duì)于將來(lái)的日期,timer樣式將遞減計(jì)數(shù)(倒計(jì)時(shí)),直到當(dāng)前時(shí)間達(dá)到指定的日期和時(shí)間為止,并在日期經(jīng)過(guò)時(shí)遞增計(jì)數(shù)(計(jì)時(shí)器)。

顯示絕對(duì)日期或時(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

顯示兩個(gè)日期之間的時(shí)間間隔

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

實(shí)現(xiàn)一天時(shí)間的計(jì)時(shí)器

  使用 style: .time樣式,如果當(dāng)前的時(shí)間比指定的時(shí)間大,則時(shí)間就會(huì)累計(jì)?;谶@個(gè)原理,我們只需要把時(shí)間起點(diǎn)定在每天的0點(diǎn)即可,根據(jù)當(dāng)前的時(shí)間計(jì)算出今天的開(kāi)始時(shí)間。以下方法可以根據(jù)12,24小時(shí)制度,獲取當(dāng)天起點(diǎn)時(shí)間。

//獲取當(dāng)天開(kāi)始的日期,給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)

結(jié)語(yǔ)

  通過(guò)IOS Text控件我們實(shí)現(xiàn)了按秒刷新的計(jì)時(shí)器,所以數(shù)字時(shí)鐘的按秒刷新算是解決了,但是怎么實(shí)現(xiàn)表盤(pán)時(shí)鐘的秒針360度旋轉(zhuǎn)呢?如果讓秒針精確的對(duì)應(yīng)當(dāng)前的時(shí)間,應(yīng)該做不到了。后面再繼續(xù)研究,如果有解決方案的可以貢獻(xiàn)一下,感謝。

相關(guān)文章

最新評(píng)論