標題: Apple Watch -- 作爲遊戲開發者的你準備好了麼? (上)
本帖最後由 Bowie 於 2015-2-16 11:57 編輯
- <font size="3">//
- // InterfaceController.h
- // WatchKitTutorial WatchKit Extension
- //
- // Created by Bowie Xu on 15/1/14.
- // Copyright (c) 2015年 CoconutIsland. All rights reserved.
- //
- #import <WatchKit/WatchKit.h>
- #import <Foundation/Foundation.h>
- @interface InterfaceController : WKInterfaceController
- @property (weak, nonatomic) IBOutlet WKInterfaceImage *watchImage;
- @property (weak, nonatomic) IBOutlet WKInterfaceButton *watchButton;
- @property (strong, nonatomic) NSArray* buttonTitles;
- @property (assign, nonatomic) int titleIndex;
- - (IBAction)WatchButtonClicked;
- @end
- </font>
- <font size="3">//
- // InterfaceController.m
- // WatchKitTutorial WatchKit Extension
- //
- // Created by Bowie Xu on 15/1/14.
- // Copyright (c) 2015年 CoconutIsland. All rights reserved.
- //
- #import "InterfaceController.h"
- @interface InterfaceController()
- @end
- @implementation InterfaceController
- - (void)awakeWithContext:(id)context {
- [super awakeWithContext:context];
- _buttonTitles = [NSArray arrayWithObjects:@"From Watch App", @"From Extension", @"From iOS App",nil];
- // Configure interface objects here.
- _titleIndex = 0;
- [self SetButtonTitle:_titleIndex];
- }
- - (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];
- }
- - (void) ShowPicFromWatchKitApp
- {
-
- }
- - (void) ShowPicFromExtension
- {
-
- }
- - (void) ShowPicFromiOSApp
- {
-
- }
- - (void) SetButtonTitle:(int)titleIndex
- {
- [_watchButton setTitle:[_buttonTitles objectAtIndex:titleIndex]];
- }
- - (void) IncreaseIndex
- {
- _titleIndex++;
- _titleIndex %= [_buttonTitles count];
- }
- - (IBAction)WatchButtonClicked {
- switch (_titleIndex) {
- case 0:
- [self ShowPicFromWatchKitApp];
- break;
- case 1:
- [self ShowPicFromExtension];
- break;
- case 2:
- [self ShowPicFromiOSApp];
- break;
- default:
- break;
- }
-
- [self IncreaseIndex];
- [self SetButtonTitle:_titleIndex];
- }
- @end
- </font>
- <font size="3">- (void) ShowPicFromWatchKitApp
- {
- [_watchImage setImageNamed:@"AppleImage.png"];
- }
- </font>
- <font size="3">- (void) ShowPicFromExtension
- {
- UIImage* image = [UIImage imageNamed:@"AppleRainbow.png"];
- [_watchImage setImage:image];
- }
- </font>
- <font size="3">+ (BOOL)openParentApplication:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo, NSError *error)) reply; // launches containing iOS application on the phone. userInfo must be non-nil
- </font>
- <font size="3">- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply NS_AVAILABLE_IOS(8_2);
- </font>
- <font size="3">-(void) _RequestData:(NSNumber*) nindex {
- //NSData* indexdata = [NSData dataWithBytes:&_AnimationIndex length:sizeof(_AnimationIndex)];
- //NSDictionary *request = [NSDictionary dictionaryWithObject:indexdata forKey:@"request"];
- int index = [nindex intValue];
- if (index>=MAXKEYS || index<0) {
- return;
- }
-
- NSDictionary *request = @{@"request":@"PIC"};
- NSString* key = [NSString stringWithFormat:@"%d", index];
-
- [_AnimationPics removeObjectForKey:key];
-
- [InterfaceController openParentApplication:request reply:^(NSDictionary *replyInfo, NSError *error) {
-
- if (error) {
- NSLog(@"%@", error);
- [_AnimationPics removeObjectForKey:key];
- } else {
- NSData* data = [replyInfo objectForKey:@"PIC"];
- NSArray* picarray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
- [_AnimationPics setObject:picarray forKey:key];
-
- //start animation
- if (_AnimationTimer == nil) {
- _AnimationTimer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(SetAnimationIndex) userInfo:nil repeats:YES];
- }
-
- }
- }];
-
- }
- iOS App中的響應代碼
- #define PICCOUNT 30
- - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
-
- if ([[userInfo objectForKey:@"request"] isEqualToString:@"PIC"]) {
-
- NSLog(@"containing app received message from watch");
-
-
- NSMutableArray* marray = [NSMutableArray arrayWithCapacity:PICCOUNT];
- for (int i=0; i<PICCOUNT; i++) {
- UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"glance-%[email protected]", i+_picindex]];
- NSData* imagedata = UIImagePNGRepresentation(image);
- [marray addObject:imagedata];
- }
-
- _picindex += PICCOUNT;
- _picindex %= 360;
-
- NSData* imageData = [NSKeyedArchiver archivedDataWithRootObject:marray];
-
- //NSData* imageData = UIImageJPEGRepresentation(image, 1.0f);
- NSDictionary *response = [NSDictionary dictionaryWithObject:imageData forKey:@"PIC"];//@{@"response" : @"Watchkit"};
-
- reply(response);
- }
- }
- </font>
WatchKit
|
UIKit
|
WKInterfaceController
|
UIViewController
|
WKUserNotificationInterfaceController
|
UIApplicationDelegate + UIAlertController
|
WKInterfaceDevice
|
UIDevice
|
WKInterfaceObject
|
UIView
|
WKInterfaceButton
|
UIButton
|
WKInterfaceDate
|
UILabel + NSDateFormatter
|
WKInterfaceGroup
|
UIScrollView
|
WKInterfaceImage
|
UIImageView
|
WKInterfaceLabel
|
UILabel
|
WKInterfaceMap
|
MKMapView
|
WKInterfaceSeparator
|
UITableView.separatorColor / .separatorStyle
|
WKInterfaceSlider
|
UIStepper + UISlider
|
WKInterfaceSwitch
|
UISwitch
|
WKInterfaceTable
|
UITableView
|
WKInterfaceTimer
|
UILabel + NSDateFormatter + NSTimer
|