如何在CLKComplicationDataSource中定义两个复杂的数据

时间:2015-10-30 17:17:28

标签: swift swift2 apple-watch apple-watch-complication clockkit

所以,我是ClockKit并发症的新手,我想知道如何为两个并发症制作占位符模板。

我的代码到目前为止:

func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
    // This method will be called once per supported complication, and the results will be cached

    let template = CLKComplicationTemplateModularLargeStandardBody()
    template.headerTextProvider = CLKTimeIntervalTextProvider(startDate: NSDate(), endDate: NSDate())
    template.body1TextProvider = CLKSimpleTextProvider(text: "Label 1", shortText: "1")
    template.body2TextProvider = CLKSimpleTextProvider(text: "Label 2", shortText: 2)

    handler(template)

}

任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:3)

根据complication.family

添加switch语句以返回特定的并发症
func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
    // This method will be called once per supported complication, and the results will be cached
    var template: CLKComplicationTemplate? = nil
    switch complication.family {
    case .ModularSmall:
        template = nil
    case .ModularLarge:
        let modularLargeTemplate = CLKComplicationTemplateModularLargeStandardBody()
        modularLargeTemplate.headerTextProvider = CLKTimeIntervalTextProvider(startDate: NSDate(), endDate: NSDate())
        modularLargeTemplate.body1TextProvider = CLKSimpleTextProvider(text: "Label 1", shortText: "1")
        modularLargeTemplate.body2TextProvider = CLKSimpleTextProvider(text: "Label 2", shortText: "2")
        template = modularLargeTemplate
    case .UtilitarianSmall:
        template = nil
    case .UtilitarianLarge:
        let utilitarianLargeTemplate = CLKComplicationTemplateUtilitarianLargeFlat()
        utilitarianLargeTemplate.textProvider = CLKSimpleTextProvider(text: "Label 1")
        template = utilitarianLargeTemplate
    case .CircularSmall:
        let circularSmallTemplate = CLKComplicationTemplateCircularSmallRingText()
        circularSmallTemplate.textProvider = CLKSimpleTextProvider(text: "1")
        circularSmallTemplate.fillFraction = 0.5
        circularSmallTemplate.ringStyle = CLKComplicationRingStyle.Closed
        template = circularSmallTemplate
    }
    handler(template)
}