检测Text.DateStyle.timer何时到达00:00

问题描述

所以我是WidgetKit和SwiftUI的新手,但是有没有一种事件或方法可以检测Text()的倒数计时何时达到00:00?

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

Text(futureDate,style: .timer)

它似乎要么文献记录不充分-至少对我来说-还是没有这样的东西。?

解决方法

不可能,当达到0时它将开始计数。

您必须使用时间线在倒数结束日期刷新该小部件,并使用仅显示0:00:00的普通文本

一个例子:

var entries: [SingleEntry] = []
                // First entry at Date() which is now... with the countdown endDate at 60 seconds in the future
                // which you'll use in the Text(date,style)
                entries.append(
                    SingleEntry(
                        date: Date(),configuration: configuration,endDate: Date().addingTimeInterval(60)
                    )
                )
                
                // Second entry which will be scheduled at the Date when you want to stop the timer
                // in the TimelineEntry now you can check if endDate is nil and use a normale text
                // to say that the countdown is over
                entries.append(
                    SingleEntry(
                        date: Date().addingTimeInterval(60),endDate: nil
                    )
                )
                
                let timeline = Timeline(entries: entries,policy: .atEnd)
                completion(timeline)