多个复杂功能系列的时间线条目

Timeline Entries for multiple Complication Families

当我只有 .ModularLarge.

时,我的复杂功能和时间旅行工作得很好

我想添加 .ModularSmall 作为一个选项,所以我尝试以几种不同的方式修改我的代码,但它不再按预期工作。

现在发生的事情是,时间旅行将对从 getTimelineEntriesForComplication 生成的数组中的第一个条目起作用,但是在进行时间旅行时,下一个条目永远不会出现,因此复杂功能只停留在第一个条目上。

时间线 尝试使用if语句:

func getTimelineEntriesForComplication(complication: CLKComplication, afterDate date: NSDate, limit: Int, withHandler handler: (([CLKComplicationTimelineEntry]?) -> Void)) {

    guard let headers = headerArray, texts = body1Array, dates = body2Array else { return }

    var entries = [CLKComplicationTimelineEntry]()
    for (index, header) in headers.enumerate() {
        let text = texts[index]
        let date1 = dates[index]
        let headerTextProvider = CLKSimpleTextProvider(text: header as! String)
        let body1TextProvider = CLKSimpleTextProvider(text: text as! String)
        let timeTextProvider = CLKTimeTextProvider(date: date1 as! NSDate)
        let template = CLKComplicationTemplateModularLargeStandardBody()
        let template2 = CLKComplicationTemplateModularSmallStackText()
        template.headerTextProvider = headerTextProvider
        template.body1TextProvider = body1TextProvider
        template2.line1TextProvider = headerTextProvider
        template2.line2TextProvider = body1TextProvider
        template.body2TextProvider = timeTextProvider

        if complication.family == .ModularLarge {
            let timelineEntry = CLKComplicationTimelineEntry(date: date1 as! NSDate, complicationTemplate: template)
            entries.append(timelineEntry)
            handler(entries)
        }
        if complication.family == .ModularSmall {
            let timelineEntry = CLKComplicationTimelineEntry(date: date1 as! NSDate, complicationTemplate: template2)
            entries.append(timelineEntry)
            handler(entries)
        }
        else {
            handler(nil)
        }
    }
}

What's happening is Time Travel will work for the first entry in the array produced from getTimelineEntriesForComplication, but the next entries never appear when doing Time Travel, so the Complication just stays on the first entry.

您的 getTimelineEntriesForComplication 代码中存在错误。您返回了一个只有一个条目的数组,因为您返回了 inside for 循环:

for (index, header) in headers.enumerate() {
    if complication.family == .ModularLarge {
        let timelineEntry = CLKComplicationTimelineEntry(date: date1 as! NSDate, complicationTemplate: template)
        entries.append(timelineEntry)
        handler(entries)
    }
}

重构您的代码,使其看起来像这样:

for (index, header) in headers.enumerate() {
    if complication.family == .ModularLarge {
        let timelineEntry = CLKComplicationTimelineEntry(date: date1 as! NSDate, complicationTemplate: template)
        entries.append(timelineEntry)
    }
}
handler(entries)