继承的是NSObject
不会管理Watch APP的应用界面
界面是直接由Watch Kit来管理
- (void)handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)localNotification { } - (void)handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification { } - 经过这两个方法得到通知的消息,跳转到目标界面
- (void)didReceiveLocalNotification:(UILocalNotification *)localNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler { } - (void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler { } - 得到通知内容,并设置处理完成的回调Block
init
:用来初始化interface ControllerawakeWithContext
:相似UIViewController中的viewDidLoad方法,用来配置interface ControllerwillActivity
:界面将要显示给用户时会被调用,主要用来对试图进行一些小的调整,大规模的初始化仍是放在init和awakeWithContextdidDeactivate
:用来清空界面,程序进入不活动状态,能够用它来终止Timer或者来中止动画,这个方法不能再对界面进行操做@implementation InterfaceController /** * 初始化当前控制器,子控制器不必定有值 * * @return 控制器 */ - (instancetype)init { if (self = [super init]) { NSLog(@"初始化当前控制器,子控制器不必定有值 %s %d", __func__, __LINE__); } return self; } /** * 建立界面完毕,能够在这个对子控制器赋值 * * @param context 界面里的内容 */ - (void)awakeWithContext:(id)context { [super awakeWithContext:context]; // Configure interface objects here. NSLog(@"建立界面完毕,能够在这个对子控制器赋值 %s %d", __func__, __LINE__); } /** * 即将进入程序,对界面作微调,具体的初始化应该放在 init 或者 awakeWithContext */ - (void)willActivate { // This method is called when watch view controller is about to be visible to user [super willActivate]; NSLog(@"即将进入程序,对界面作微调,具体的初始化应该放在 init 或者 awakeWithContext %s %d", __func__, __LINE__); } /** * 清空界面,通常是用来中止动画:timer */ - (void)didDeactivate { // This method is called when watch view controller is no longer visible [super didDeactivate]; NSLog(@"清空界面,通常是用来中止动画:timer %s %d", __func__, __LINE__); } @end
- (void)awakeWithContext:(id)context { [super awakeWithContext:context]; // Configure interface objects here. // 设置标签的文字 [self.label setText:@"我是一个标签"]; // 设置标签的文字颜色 [self.label setTextColor:[UIColor redColor]]; }
- (void)awakeWithContext:(id)context { [super awakeWithContext:context]; // Configure interface objects here. // 设置标签文字 [self.label setText:@"文字已经被改变"]; // 设置标签文字颜色 [self.label setTextColor:[UIColor blueColor]]; // 设置按钮不可点 [self.button setEnabled:NO]; // 设置按钮文字 [self.button setTitle:@"不可dian"]; // 设置按钮背景图片 [self.button setBackgroundImage:[UIImage imageNamed:@"ad_00"]]; }
- 代码实现
- (void)awakeWithContext:(id)context { [super awakeWithContext:context]; // Configure interface objects here. [self.image setImageNamed:@"ad_01.png"]; }
/** * 上 */ - (IBAction)up { // 设置动画时长2.0秒 [self animateWithDuration:2.0 animations:^{ // 设置控件 垂直方向的位置 [self.ball setVerticalAlignment:WKInterfaceObjectVerticalAlignmentTop]; }]; } /** * 下 */ - (IBAction)down { [self animateWithDuration:2.0 animations:^{ [self.ball setVerticalAlignment:WKInterfaceObjectVerticalAlignmentBottom]; }]; } /** * 左 */ - (IBAction)left { [self animateWithDuration:2.0 animations:^{ // 设置控件 水平方向的位置 [self.ball setHorizontalAlignment:WKInterfaceObjectHorizontalAlignmentLeft]; }]; } /** * 右 */ - (IBAction)rigth { [self animateWithDuration:2.0 animations:^{ [self.ball setHorizontalAlignment:WKInterfaceObjectHorizontalAlignmentRight]; }]; }
/** * push方式跳转控制器 */ - (IBAction)push { /** * name: 给控制器绑定一个Identifier:pushCtr */ [self pushControllerWithName:@"pushCtr" context:nil]; } /** * moda方式跳转控制器 */ - (IBAction)moda { [self presentControllerWithName:@"modaCtr" context:nil]; }
// iOS UIDevice *device = [UIDevice currentDevice]; // watch OS WKInterfaceDevice *interfaceDevice = [WKInterfaceDevice currentDevice]; // 添加缓存图片 (过程当中可能会添加失败)2 [interfaceDevice addCachedImage:[UIImage imageNamed:@"ad_00"] name:@"Zeng01"]; [interfaceDevice addCachedImage:[UIImage imageNamed:@"ad_01"] name:@"Zeng02"]; /** * 会先判断图片是否子啊缓存区内,若是在,直接取出来使用 * 若是不在缓存区,就建立一个名字为 ad_00 的图片 * 每个watch App 的缓存区最大不超过20M * 若是超过了20M,那么就一次从最开始的缓存图片开始删除,用来存放新的缓存图片 */ [self.showImage setImageNamed:@"ad_00"]; // 从缓存区删除缓存图片 [interfaceDevice removeCachedImageWithName:@"zeng02"]; NSLog(@"全部的缓存图片 = %@", interfaceDevice.cachedImages); /** * 获取屏幕的大小 * * 在42mm 的手表下 屏幕大小是 : 宽:156 高: 195 312 X 390 资源图片的像素 * 在38mm 的手表下 屏幕大小是 : 宽: 136 高: 170 272 X 340 资源图片的像素 */ NSLog(@"当前屏幕的大小为 = %@, 当前屏幕能够缩放的比例 = %f", NSStringFromCGRect(interfaceDevice.screenBounds), interfaceDevice.screenScale);
显示效果git
// 数据共享 // iOS NSUserDefaults *iOSDefaults = [NSUserDefaults standardUserDefaults]; [iOSDefaults setObject:@"xmgShareData" forKey:@"xmgKey"]; // watch OS // 1. 打开target中 app Groups (iOS 上的 和 watch OS 上的) //1.1 打开成功以后 须要你写一个名字 // 2. 使用如下方式建立数据库 NSUserDefaults *watchOSDefaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.xmg"]; [watchOSDefaults setObject:@"网络太差" forKey:@"xmgKey"];
ShowCell.hgithub
@interface ShowCell : NSObject - (void)setImageName:(NSString *)imageName title:(NSString *)title; @end
showCell.m数据库
#import "ShowCell.h" #import <WatchKit/WatchKit.h> @interface ShowCell () @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceImage *image; @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *label; @end @implementation ShowCell - (void)setImageName:(NSString *)imageName title:(NSString *)title { [self.image setImageNamed:imageName]; [self.label setText:title]; } @end
InterfaceController.m浏览器
@interface InterfaceController() @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceTable *tableView; @end @implementation InterfaceController - (void)awakeWithContext:(id)context { [super awakeWithContext:context]; // Configure interface objects here. [self.tableView setNumberOfRows:5 withRowType:@"row1"]; for (NSUInteger i = 0; i < 5; i++) { // 获取第i行的row ShowCell *cell = [self.tableView rowControllerAtIndex:i]; // 赋值 [cell setImageName:[NSString stringWithFormat:@"ad_0%d", i] title:[NSString stringWithFormat:@"第%d个", i + 1]]; } } /** * 点击了某行tableView */ - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex { ShowCell *cell = [self.tableView rowControllerAtIndex:rowIndex]; NSLog(@"%s", __func__); } - (void)willActivate { // This method is called when watch view controller is about to be visible to user [super willActivate]; } - (void)didDeactivate { // This method is called when watch view controller is no longer visible [super didDeactivate]; } /** * 添加一行 */ - (IBAction)addRow { // 获取tableView的最后一行 NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:self.tableView.numberOfRows]; // 插入一行 [self.tableView insertRowsAtIndexes:indexSet withRowType:@"row1"]; // 滚到哪一行 [self.tableView scrollToRowAtIndex:0]; } /** * 删除一行 */ - (IBAction)deleteRow { // 获取tableView的最后一行 NSIndexSet *lastIndexSet = [NSIndexSet indexSetWithIndex:self.tableView.numberOfRows - 1]; // 删除最后一行 [self.tableView removeRowsAtIndexes:lastIndexSet]; } @end
@interface InterfaceController() @property (unsafe_unretained, nonatomic) IBOutlet WKInterfacePicker *listPicker; @property (unsafe_unretained, nonatomic) IBOutlet WKInterfacePicker *stackPicker; @property (unsafe_unretained, nonatomic) IBOutlet WKInterfacePicker *sequencePicker; @end @implementation InterfaceController - (void)awakeWithContext:(id)context { [super awakeWithContext:context]; // Configure interface objects here. WKPickerItem *item1 = [[WKPickerItem alloc] init]; item1.title = @"第1个"; item1.contentImage = [WKImage imageWithImage:[UIImage imageNamed:@"ad_00"]]; item1.caption = @"薯片真好吃"; WKPickerItem *item2 = [[WKPickerItem alloc] init]; item2.title = @"第2个"; item2.contentImage = [WKImage imageWithImage:[UIImage imageNamed:@"ad_01"]]; item2.caption = @"饼干真好吃"; WKPickerItem *item3 = [[WKPickerItem alloc] init]; item3.title = @"第3个"; item3.contentImage = [WKImage imageWithImage:[UIImage imageNamed:@"ad_02"]]; item3.caption = @"水疗"; WKPickerItem *item4 = [[WKPickerItem alloc] init]; item4.title = @"第4个"; item4.contentImage = [WKImage imageWithImage:[UIImage imageNamed:@"ad_03"]]; item4.caption = @"汤真美味"; WKPickerItem *item5 = [[WKPickerItem alloc] init]; item5.title = @"第5个"; item5.contentImage = [WKImage imageWithImage:[UIImage imageNamed:@"ad_04"]]; item5.caption = @"餐厅"; [self.listPicker setItems:@[item1, item2, item3, item4, item5]]; [self.stackPicker setItems:@[item1, item2, item3, item4, item5]]; [self.sequencePicker setItems:@[item1, item2, item3, item4, item5]]; }
- (IBAction)showAlertView { WKAlertAction *doneAction = [WKAlertAction actionWithTitle:@"done" style:WKAlertActionStyleDefault handler:^{ NSLog(@"done"); }]; WKAlertAction *destructiveAction = [WKAlertAction actionWithTitle:@"具备破坏性的" style:WKAlertActionStyleDestructive handler:^{ NSLog(@"具备破坏性的"); }]; WKAlertAction *cancelAction = [WKAlertAction actionWithTitle:@"exit" style:WKAlertActionStyleCancel handler:^{ NSLog(@"cancel"); }]; /** * 三种显示方式 * WKAlertControllerStyleAlert, * WKAlertControllerStyleSideBySideButtonsAlert, * WKAlertControllerStyleActionSheet, */ // Action依次排序的 // [self presentAlertControllerWithTitle:@"Demo" message:@"Action依次排序的" preferredStyle:WKAlertControllerStyleAlert actions:@[doneAction, destructiveAction, cancelAction]]; // 只须要两个Action 一个Action就至关于一个按钮,不然报错 // [self presentAlertControllerWithTitle:@"Demo" message:@"只须要两个Action 一个Action就至关于一个按钮,不然报错" preferredStyle:WKAlertControllerStyleSideBySideButtonsAlert actions:@[doneAction, cancelAction]]; // 退出在左上角 [self presentAlertControllerWithTitle:@"Demo" message:@"退出在左上角" preferredStyle:WKAlertControllerStyleActionSheet actions:@[doneAction, destructiveAction, cancelAction]]; }
/** * 上移 */ - (IBAction)moveUp { [self animateWithDuration:2.0f animations:^{ [self.ball setVerticalAlignment:WKInterfaceObjectVerticalAlignmentTop]; }]; } /** * 下移 */ - (IBAction)moveDown { [self animateWithDuration:2.0f animations:^{ [self.ball setVerticalAlignment:WKInterfaceObjectVerticalAlignmentBottom]; }]; } /** * 左移 */ - (IBAction)moveLeft { [self animateWithDuration:2.0f animations:^{ [self.ball setHorizontalAlignment:WKInterfaceObjectHorizontalAlignmentLeft]; }]; } /** * 右移 */ - (IBAction)moveRight { [self animateWithDuration:2.0f animations:^{ [self.ball setHorizontalAlignment:WKInterfaceObjectHorizontalAlignmentRight]; }]; } /** * 缩放 */ - (IBAction)MoveZoom { [self animateWithDuration:2.0f animations:^{ [self.ball setRelativeWidth:0.8 withAdjustment:10]; [self.ball setRelativeHeight:0.4 withAdjustment:10]; }]; }
显示效果缓存
  