当我只有.ModularLarge
时,我的并发症和时间旅行完美无缺。
我想添加.ModularSmall
作为选项,因此我尝试以几种不同的方式修改我的代码,但它不再按预期工作。
现在发生的事情是,Time Travel将适用于getTimelineEntriesForComplication
生成的阵列中的第一个条目,但是在进行Time Travel时,下一个条目永远不会出现,因此Complication只停留在第一个条目上条目。
时间轴 尝试使用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)
}
}
}
答案 0 :(得分:1)
Time Travel将适用于getTimelineEntriesForComplication生成的数组中的第一个条目,但是在执行Time Travel时,下一个条目永远不会出现,因此Complication只停留在第一个条目上。
您的getTimelineEntriesForComplication
代码中存在错误。您已经返回了一个只包含一个条目的数组,因为您在 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)