繼承的是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]; }]; }
顯示效果緩存
![](http://images2015.cnblogs.com/blog/1002304/201608/1002304-20160820212018531-772209631.png) ![](http://images2015.cnblogs.com/blog/1002304/201608/1002304-20160820212025968-1691369795.png) ![](http://images2015.cnblogs.com/blog/1002304/201608/1002304-20160820212033453-588989304.png)