watchOS2支持自定义表盘的Complication(苹果把表盘上显示的每一个控件称之为Complication)。苹果已经做使定制Complication在watchOS2的非常好的工作。WatchoOS现在包含ClockKit(表盘开发工具包),他包含了所有可定制化的Complication类型。自定义表盘的工作方式是非常简单的,只需要你的Watch Extension提供实现CLKComplicationDataSource协议,提供给ClockKit(表盘)数据。
Complication的五大分类:
一张图解释清楚:
Complication 布局
提供给表盘(ClockKit)显示元素主要是通过CKComplicationTemplate,每一种家族的表盘元素 (complication)都有特定的子类,共提供用23个子类:
通过自己的业务需求来选择数据展示所需模版。
Complication的Provider:
可以通过选择上面的模版来展示我们的数据。那么我们的数据如何显示在模版上的呢?我们可以通过设置模版的Providers(提供者),Providers是非常灵活的,提供了各种各样的Provider,如需要显示文本内容,我们有CLKTextProvider,它的子类CLKSimpleTextProvider非常常用,它有个text属性,设置这个属性提供给模版显示简单的文本内容。显示图片用CLKImageProvider提供给模版显示简单图片。使用CLKDateTextProvider,CLKTimeTextProvider和relativCLKRelativeDateTextProvider来为表盘显示时间。
/1.创建选择的模版
let tmpl = CLKComplicationTemplateModularLargeStandardBody()
//2.使用Provider设置要显示的数据
tmpl.headerImageProvider = CLKImageProvider(onePieceImage:UIImage(named: "soccer_ball")!)
tmpl.headerTextProvider = CLKTimeTextProvider(date: match.date!)
tmpl.body1TextProvider = CLKSimpleTextProvider(text: match.teamDesc!)
tmpl.body2TextProvider = CLKSimpleTextProvider(text: match.groupDesc!)
自动生成的:CLKComplicationDataSource:
@implementation ComplicationController #pragma mark - Timeline Configuration //时间线前后 - (void)getSupportedTimeTravelDirectionsForComplication:(CLKComplication *)complication withHandler:(void(^)(CLKComplicationTimeTravelDirections directions))handler { handler(CLKComplicationTimeTravelDirectionForward|CLKComplicationTimeTravelDirectionBackward); } //时间线起点 - (void)getTimelineStartDateForComplication:(CLKComplication *)complication withHandler:(void(^)(NSDate * __nullable date))handler { handler(nil); } //时间线终点 - (void)getTimelineEndDateForComplication:(CLKComplication *)complication withHandler:(void(^)(NSDate * __nullable date))handler { handler(nil); } //在锁屏时候是否隐藏Complication - (void)getPrivacyBehaviorForComplication:(CLKComplication *)complication withHandler:(void(^)(CLKComplicationPrivacyBehavior privacyBehavior))handler { handler(CLKComplicationPrivacyBehaviorShowOnLockScreen); } #pragma mark - Timeline Population //获取当前complication - (void)getCurrentTimelineEntryForComplication:(CLKComplication *)complication withHandler:(void(^)(CLKComplicationTimelineEntry * __nullable))handler { // Call the handler with the current timeline entry handler(nil); } //获取limit个时间点前的complication - (void)getTimelineEntriesForComplication:(CLKComplication *)complication beforeDate:(NSDate *)date limit:(NSUInteger)limit withHandler:(void(^)(NSArray<CLKComplicationTimelineEntry *> * __nullable entries))handler { // Call the handler with the timeline entries prior to the given date handler(nil); } //获取limit个时间点后的complication - (void)getTimelineEntriesForComplication:(CLKComplication *)complication afterDate:(NSDate *)date limit:(NSUInteger)limit withHandler:(void(^)(NSArray<CLKComplicationTimelineEntry *> * __nullable entries))handler { // Call the handler with the timeline entries after to the given date handler(nil); } #pragma mark Update Scheduling //多久更新时间线 - (void)getNextRequestedUpdateDateWithHandler:(void(^)(NSDate * __nullable updateDate))handler { // Call the handler with the date when you would next like to be given the opportunity to update your complication content handler(nil); } #pragma mark - Placeholder Templates //complication的placeholder - (void)getPlaceholderTemplateForComplication:(CLKComplication *)complication withHandler:(void(^)(CLKComplicationTemplate * __nullable complicationTemplate))handler { // This method will be called once per supported complication, and the results will be cached handler(nil); } @end
感谢:
https://developer.apple.com/library/watchos/documentation/General/Conceptual/AppleWatch2TransitionGuide/DesigningaComplication.html#//apple_ref/doc/uid/TP40015234-CH11-SW1
http://blog.csdn.net/wangjavafans/article/details/47763423