WatchOS 并发症

时间:2021-06-22 10:27:32

标签: ios swift apple-watch watchos apple-watch-complication

我有一个包含 10 个事件的数组,假设当前时间是上午 10 点,而事件将在上午 11 点开始。 每个事件为 30 分钟,两个事件之间的间隔为 30 分钟。

现在,我可以正确显示表盘上的所有必需信息,即当表盘启动时,它会显示下一个活动是在上午 11 点,当第一个活动在上午 11:30 结束时,上午 11:31 表盘显示下一场活动将于下午 12 点开始。

我能够成功地完成这一切。

我想在开始时间前 15 分钟显示第一个活动的提醒时遇到问题。

例如,如果一天中的第一个事件将在上午 11 点开始,那么表盘将仅在上午 10:45 而不是在上午 10:45 之前显示该第一个事件的提醒。

另外,当一天的最后一个事件结束后,我如何才能移除复杂功能?

我已经看过 Complications WWDC - 2015 的视频。

我正在尝试在 CLKComplicationTemplateModularLargeStandardBody 上显示 Complication。

    private func getTimeLineEntry() -> Date {
    
     if shouldShowCountDown {
         return startDate.addingTimeInterval(-15 * 60)
     } else {
         return startDate
     }
    }

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
    // Call the handler with the current timeline entry

      let headerTextProvider = CLKSimpleTextProvider(text: "Complication Updates")
            let body1 = "My Title"
            let body1TextProvider = CLKSimpleTextProvider(text: body1)
            let body2TextProvider = CLKSimpleTextProvider(text: "My Subtitle")
    
            let template = CLKComplicationTemplateModularLargeStandardBody(headerTextProvider: headerTextProvider, body1TextProvider: body1TextProvider, body2TextProvider: body2TextProvider)
    
            let entry = CLKComplicationTimelineEntry(date: getTimeLineEntry(), complicationTemplate: template)
            handler(entry)
    
}

这里,startDate 是上午 11 点,我想在活动开始前 15 分钟(即上午 10:45)显示有关并发症的提醒。我使用了 bool 变量来决定当前时间是否为 startDate > Current Date。如果是,则在 15 分钟前显示倒计时。

1 个答案:

答案 0 :(得分:0)

此问题的解决方法是 - 我们需要将第一个模板设为空模板。

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
    // Call the handler with the current timeline entry
    let headerTextProvider = CLKSimpleTextProvider(text: "")
    let body1TextProvider = CLKSimpleTextProvider(text: "")
    let body2TextProvider = CLKSimpleTextProvider(text: "")
    let template = CLKComplicationTemplateModularLargeStandardBody(headerTextProvider: headerTextProvider, body1TextProvider: body1TextProvider, body2TextProvider: body2TextProvider)
    let entry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
    handler(entry)
}

因此,即使在任何时间启动复杂功能,它也会向用户显示空白模板,当时间是上午 10:45 时,它将开始在 WatchFace 上显示第一个复杂功能。

func getTimelineEntries(for complication: CLKComplication, after date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
    // Call the handler with the timeline entries after the given date
    guard !array.isEmpty else {
        handler(nil)
        return
    }
    
    var entries: [CLKComplicationTimelineEntry] = []
    
    for (index, arrayObj) in array.enumerated() {
        var nextDate = Date()
        nextDate = (index == 0) ? (arrayObj.startDate).addingTimeInterval(TimeInterval(-15 * 60)) : arrayObj.startDate
        
        let headerTextProvider = CLKSimpleTextProvider(text: "Event Update")
    let body1TextProvider = CLKSimpleTextProvider(text: "Event will start at")
    let body2TextProvider = Date(timeInterval: arrayObj.interval, since: arrayObj.startDate)
    let template = CLKComplicationTemplateModularLargeStandardBody(headerTextProvider: headerTextProvider, body1TextProvider: body1TextProvider, body2TextProvider: body2TextProvider)
        let entry = CLKComplicationTimelineEntry(date: nextDate, complicationTemplate: template)
        entries.append(entry)

        if entries.count == limit { break }
    }
    handler(entries)
}
相关问题