使用Objective-C开发AppleWatch(一)关于Complication

来源:互联网 发布:.xyz域名查询 编辑:程序博客网 时间:2024/04/30 05:12

使用Objective-C开发AppleWatch(一)关于Complication (持续更新)

现在用Swift开发appleWatch的教程比较多,而比起Swift,Objective-C仍然是Cocoa框架开发的主力语言,所以在这里,介绍给大家如何用Objective-C来开发一个我们的appleWatch Complication(表盘)

什么是Complication开发,AppleWatch表盘模块有哪些种类

分类 模块化表盘小组件 模块化表盘大组件 实用表盘小组件 实用表盘大组件 多彩表盘组件 示意图 模块化表盘小组件 模块化表盘大组件 使用表盘小组件 实用表盘大组件 多彩表盘组件

下面我们就要把我们的应用上的一些信息,显示到表盘中去,用这5种显示的形态

如何开始我们的表盘的开发工作

一、新建一个带有Complication的AppleWatch工程

新建一个带有Complication表盘项目的工程
watchOSApp依附于 iOSApp,这次介绍的watchOSApp是建立在一个iOSApp上的。

二、Complication这个类中实现的方法简介

complication这个类是表盘的数据源类,它继承的是

@interface ComplicationController : NSObject <CLKComplicationDataSource>

在这个类中的方法主要分为三个部分
①timeLine的设定部分

//是否支持timeTravel,和支持timeTravel的方向(时间的前进和后退)- (void)getSupportedTimeTravelDirectionsForComplication:(CLKComplication *)complication withHandler:(void (^)(CLKComplicationTimeTravelDirections directions))handler {    handler(CLKComplicationTimeTravelDirectionForward | CLKComplicationTimeTravelDirectionBackward);}
//支持timeTravel的开始时间- (void)getTimelineStartDateForComplication:(CLKComplication *)complication withHandler:(void (^)(NSDate *__nullable date))handler {    handler(nil);}
//支持timeTravel的结束时间- (void)getTimelineEndDateForComplication:(CLKComplication *)complication withHandler:(void (^)(NSDate *__nullable date))handler {    handler(nil);}
//是否支持在锁屏期间查看- (void)getPrivacyBehaviorForComplication:(CLKComplication *)complication withHandler:(void (^)(CLKComplicationPrivacyBehavior privacyBehavior))handler {    handler(CLKComplicationPrivacyBehaviorShowOnLockScreen);}

②timeLine的数据源部分

//返回当前时间的entry- (void)getCurrentTimelineEntryForComplication:(CLKComplication *)complication withHandler:(void (^)(CLKComplicationTimelineEntry *__nullable))handler {   handler(nil);}
//返回向过去TimeTravel时要展示的一个entry数组- (void)getTimelineEntriesForComplication:(CLKComplication *)complication beforeDate:(NSDate *)date limit:(NSUInteger)limit withHandler:(void (^)(NSArray <CLKComplicationTimelineEntry *> *__nullable entries))handler {   handler(nil);}
//返回向未来TimeTravel时要展示的一个entry数组- (void)getTimelineEntriesForComplication:(CLKComplication *)complication afterDate:(NSDate *)date limit:(NSUInteger)limit withHandler:(void (^)(NSArray <CLKComplicationTimelineEntry *> *__nullable entries))handler {   handler(nil);}

③更新数据部分

//在你想更新你的数据的时候调用- (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);}

④placeHolder的显示内容

//在选取小组件的时候的没有内容的预览(placeholder)- (void)getPlaceholderTemplateForComplication:(CLKComplication *)complication withHandler:(void (^)(CLKComplicationTemplate *__nullable complicationTemplate))handler {    handler(nil);}

三、编写placeholder要显示的内容

//官方表示这个placeholder只有在app被更新或重装的时候才会更换- (void)getPlaceholderTemplateForComplication:(CLKComplication *)complication withHandler:(void (^)(CLKComplicationTemplate *__nullable complicationTemplate))handler {    // 首先判断进入这个消息时表盘组件是什么,根据不同的表盘,返回不同的template    if (complication.family == CLKComplicationFamilyUtilitarianLarge) {    //截止到日前,template已经有了24个种类,点入查看可以看到具体的枚举类数量        CLKComplicationTemplateUtilitarianLargeFlat *template = [[CLKComplicationTemplateUtilitarianLargeFlat alloc]init];        template.textProvider = [CLKSimpleTextProvider textProviderWithText:@"AppLogo"];        handler((CLKComplicationTemplate *)template);    }    handler(nil);}

关于provider:provider是一个集合,一个种类,text provider就相当于label,image provider就相当于imageView,使用起来非常的便捷可靠,因为表盘的显示也比较的简洁和固定,所以我们只需要选用某一种类型,然后赋上值就可以了

四、编写当前时间要显示的内容

这一块可以说是最为重要的,就是当前时间,在你的表盘上,你的app需要在表盘中显示的内容,需要返回的是一个NSDate类型的日期变量和一个CLKComplicationTimelineEntry类型的变量,截止到日前,这个变量已经有24个种类供使用

- (void)getCurrentTimelineEntryForComplication:(CLKComplication *)complication withHandler:(void (^)(CLKComplicationTimelineEntry *__nullable))handler {    // 用handler传入参数entries来处理当前的表盘信息    if (complication.family == CLKComplicationFamilyUtilitarianLarge) {        //把创建表盘的内容提取成一个方法(templateForUtilitarianLarge)        CLKComplicationTimelineEntry *entries = [CLKComplicationTimelineEntry entryWithDate:[NSDate date] complicationTemplate:[self templateForUtilitarianLarge]];        handler(entries);    } else {        handler(nil);    }}

创建一个CLKComplicationTimelineEntry类型的变量

- (CLKComplicationTemplate *) templateForUtilitarianLarge {    CLKComplicationTemplateUtilitarianLargeFlat *template = [[CLKComplicationTemplateUtilitarianLargeFlat alloc]init];    //判断是否登录的方法,类方法,详情如下    if ([User sharedUser].isLocalLgin) {        //在AppleWatch上进行网络请求可能不是那么快速,所以在iOS上把数据存入NSUserDefault,这里取出会更好        NSString *thisDayString = [[NSUserDefaults standardUserDefaults] objectForKey:@"thisDayString"];        if(thisDayString==nil){            template.textProvider = [CLKSimpleTextProvider textProviderWithText:@"正获取数据"];        }else{            template.textProvider = [CLKSimpleTextProvider textProviderWithText:[NSString stringWithFormat:@"今日用电:%@ 度", thisDayString]];        }    } else {        template.textProvider = [CLKSimpleTextProvider textProviderWithText:@"点我登录"];    }    return template;}

这个方法放在User类中,标记了dispatch_once_t所以只会被运行一次,所以作为类方法来获取一个单例了的变量,在这个方法中做一个网络的交互,和网络请求获取登录情况,来判断是否登录

+ (instancetype)sharedUser {    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        _user = [[self alloc] init];        _user.userInfoDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"privateKey"];        [_user registListenForLogin];    });    return _user;}

五、关于流程

Created with Raphaël 2.1.0开始流程进行网络请求,把得到的结果存入NSUserDefaults(包括登录情况和数据)判断是否经登录?创建template对象在界面上展示数据完成流程创建template对象在界面上提示登录yesno

以上就完成了一个简单的基于iOSapp的Watch OS 表盘组件的建立

0 0