WatchOS4显示复杂的一秒钟,然后用空白显示替换它

时间:2017-12-07 01:11:35

标签: ios watchkit

我是iOS编程的初学者。我试图在苹果手表的复杂功能上进行样品显示,但它只显示一秒钟,之后变为空白。

这是我在ComplicationController中的代码

func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
    // This method will be called once per supported complication, and the results will be cached
    var template: CLKComplicationTemplate?
    switch complication.family {
    case .modularSmall:
        let modularSmallTemplate =
            CLKComplicationTemplateModularSmallRingText()
        modularSmallTemplate.textProvider =
            CLKSimpleTextProvider(text: "R")
        modularSmallTemplate.fillFraction = 0.75
        modularSmallTemplate.ringStyle = CLKComplicationRingStyle.closed
        template = modularSmallTemplate
    case .modularLarge:
        let modularLargeTemplate =
            CLKComplicationTemplateModularLargeStandardBody()
        modularLargeTemplate.headerTextProvider = CLKSimpleTextProvider(text: "Sample title", shortText: "Title")
        modularLargeTemplate.body1TextProvider =
            CLKSimpleTextProvider(text: "Sample Text",
                                  shortText: "Text")
        modularLargeTemplate.body2TextProvider =
            CLKSimpleTextProvider(text: "Tap to open",
                                  shortText: "Tap")
        template = modularLargeTemplate
    case .utilitarianSmall:
        template = nil
    case .utilitarianLarge:
        template = nil
    case .circularSmall:
        template = nil

    case .utilitarianSmallFlat:
        template = nil

    case .extraLarge:
        template = nil
    }
    handler(template)

}

我还在func getTimelineEntries中阅读了有关此问题的答案,我尝试在模板上粘贴相同的代码,但结果仍然相同,显示为空白。

是的,有人能帮帮我吗?谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

您所拥有的代码仅为复杂功能提供了示例模板。这仅用于并发症列表,因此被称为样本。如果要在选择复杂功能后显示数据,则需要使用getCurrentTimelineEntry函数。

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
     // Example Switch Statement
     switch complication.family {
    case .modularSmall:
        let template = CLKComplicationTemplateModularSmallSimpleImage()
        template.imageProvider.tintColor = UIColor.color(fromHexString: "#81BE41")
        template.imageProvider = CLKImageProvider(onePieceImage: backImage, twoPieceImageBackground: backImage, twoPieceImageForeground: transImage)
        handler(template)
    case .utilitarianLarge:
        let template = CLKComplicationTemplateUtilitarianLargeFlat()

        template.textProvider = CLKSimpleTextProvider(text: "Header Text")

        handler(template)

    case .modularLarge:
        let template = CLKComplicationTemplateModularLargeStandardBody()
        template.headerTextProvider = CLKSimpleTextProvider(text: "Header Text")
        template.body1TextProvider = CLKSimpleTextProvider(text: "----", shortText: "----")
        template.body2TextProvider = CLKSimpleTextProvider(text: "----", shortText: "----")

        handler(template)
    default:
        handler(nil)
    }
}

还要确保调用getTimelineStartDate()和getTimeLineEndDate()

func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {

    let date = Calendar.current.startOfDay(for: Date())
    handler(date)
}

func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {

    var  date = Calendar.current.startOfDay(for: Date())
    date = Calendar.current.date(byAdding: .day, value: 2, to: date)!
    handler(date)
}