Watch OS 2 Complication开发教程(三)基本数据(持续连载中)

来源:互联网 发布:mac拼音切换 编辑:程序博客网 时间:2024/05/06 23:06

那么经过讲解,我们来看一看怎么实现数据的传输

首先我们先在最后面加入一个帮助函数

private func templateForWatch() ->CLKComplicationTemplate{        let template = CLKComplicationTemplateModularLargeStandardBody()        template.headerImageProvider = CLKImageProvider(onePieceImage: UIImage(named: "x2")!)        template.headerTextProvider = CLKSimpleTextProvider(text: "TitleHello")        template.body1TextProvider = CLKSimpleTextProvider(text: "HelloWorld, the content is not blank any more")        return template    }

如果不明觉厉的话请看我的上一篇Blog

里面的

        template.headerTextProvider = CLKSimpleTextProvider(text: "TitleHello")        template.body1TextProvider = CLKSimpleTextProvider(text: "HelloWorld, the content is not blank any more")

可以在text:后面传值,只要传的是一个字符串(String)即可

那么完成了这个函数后我们找到getCurrentTimelineEntryForComplication这个函数

往里面加入以下代码

        let entries = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: templateForWatch())        handler(entries)

这是什么呢?

我们首先构造一个CLKCompliactionTimelineEntry常量,包含一个NSDate和一个Template(也就是我们的templateForWatch返回的Template)

然后把这个常量传给handler

大功告成!

PS:因为这个Complication目前没有任何的在时间改变时改变内容的操作,我们可以在getSupportedTimeTravelDirectionsForComplication函数中这样写

func getSupportedTimeTravelDirectionsForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimeTravelDirections) -> Void) {        handler([.None])    }

这样的话就不支持timeTravel啦


0 0