WKInterfaceController类是实现watch app界面,除了不能管理视图,WKInterfaceController和UIViewController功能相似。它运行在WatchKit extension中和远程管理app storyboard 中的控件 ,WKInterfaceController子类实现它的方法去改变storyboard控件配置和响控件的交互。
一、简介
界面控制器运行在watch app中,但是它是分开管理界面的,当要想通过代码去修改控件,要通过与storyboard连线,这样能做到代码和界面同步。
二、初始化界面控制器
当用户用app时,系统会自动启动extension文件并创建合适的控制器。App 用不同的界面控制器去管理它们的glance、通知和它的界面。WatchKit 从storyboard中得到信息决定加载那一个界面控制器,Glance和通知场景需要配置,因此系统能识别它。对于app,WatchKit加载app 的主面界面是在初始化的时候,但可以在程序启动时初始化的控制器。
当创建界面,WatchKit实例化对象和调用它的init方法,紧跟其后是awakeWithContext:
方法。用上面的方法去初始化变量和加载数据以及storyboard中控件的参数,若WatchKit在awakeWithContext:方法中拿到storyboard中的控件,可以在此方法中自定义控件相关操作。
willActivate
方法可以界面处于活动状态,willActivate可以实现最后要做的事情,如更新app视图(不要在此方中初始控件),当app 界面不能屏幕上,willActivate可能会被调用。比例, WatchKit可能调用此方法,因此可以让我们更新app, WatchKit调用didAppear方法可以让我们知道界面已经出现。当界面移除屏幕时,WatchKit会调用 willDisappear
和 didDeactivate
方法。
**重要
在初始化的时候和界面是活动状态,界面控制器才能改变其界面。一旦系统调用 didDeactivate方法,任何尝试改变控件的属性将会被忽略,直到界面控制器的willActivate方法再次被调用。在iOS模拟器, 当我们锁住模拟器(Hardware > Lock),WatchKit为当前的界面调用 didDeactivate方法。当模拟器随后被解锁,WatchKit调用willActivate方法,我们可以这种方法去测试activation和deactivation代码。
三、水平展示App
Watch app 展示可能用一个或多个导航样式。水平导航可以让app push 和 pop 控制器界面和iOS 的导航控制器相似,分页控制器可以在多个控制器间手动滑动界面。对于分页控制器,一般可以有在storyboard中拉线,一旦设置完成,当用户滑动时,系统会自动展示下一页当要改变当前展示的页数调用reloadRootControllersWithNames:contexts:
类方法。调用此该方法要比app启动早些,或者调用此方法去重新加载界面。
对于水平导航,可以根据用户的响应事件去push一个新的控制器。当要展示新的控制器,调用pushControllerWithName:context:方法和在storyboard中给新控制器一个标识。WatchKit为展示新的控制器一个水平的动画。当跳转到一个新的控制器,可以通过pushControllerWithName:context:方法传值。context对象是主要与新的控制器通讯的对象,比喻,我们想用新的控制器特定的数据,context对象可以将我们的数据传递给新的控制器,当用连线跳转控制器,WatchKit会调用 contextForSegueWithIdentifier:inTable:rowIndex:
或者contextForSegueWithIdentifier:方法,通过context对象给新的界面控制器赋值。
四、模态展示App 界面
App 可以通过模态展示界面,模态展示界面动画是从底部向上,最后填满整个屏幕,消失是动画是从顶部到底部动画,原先的控制器会再次出现。当模态展示界面,可以展示单个界面或者可以展示多个界面,调用 presentControllerWithName:context:
和 presentControllerWithNames:contexts 方法模态展示界面。模态展示的界面控制器没有标题,系统默认是Cancel,我们可以通过 setTitle:方法为新模态展示的控制命名。当用户点击标题量,WatchKit会为返回到上一个控制器
四、展示标准的系统界面
系统支持多个标准的界面输入和展示特定的信息
- 提示动作提示框
用presentAlertControllerWithTitle:message:preferredStyle:actions:方法去展示提示框和提醒用户在标准提示框中输入内容
-
语音输入
调用 presentTextInputControllerWithSuggestions:allowedInputMode:completion:接受用户输入的信息,用户可以用口述和可以选择界面上的信息 -
音频和视频播放
调用 presentMediaPlayerControllerWithURL:options:completion:
去播放音频和视频 -
录音
调用
presentAudioRecorderControllerWithOutputURL:preset:options:completion:方法可以展示录音界面。 -
通行证
调用
presentAddPassesControllerWithPasses:completion:
方法展示界面,在苹果表上增加通行证。
标准的界面有内置按钮,用来让用户随时消失界面,大部分界面有消失方法,用户可用来关闭界面。
五、WKInterfaceController属性和方法
@interface WKInterfaceController : NSObject
//初始化
- (instancetype)init NS_DESIGNATED_INITIALIZER;
//初始完界面控制器,会走此方法。在push/modal 控制器context 是上一个控制器传过的数据,默认是nil
- (void)awakeWithContext:(nullable id)context; // context from controller that did push or modal presentation. default does nothing
//界面控制器
@property (nonatomic, readonly) CGRect contentFrame;
//不太明白这个方法干嘛的????
@property (nonatomic, strong, readonly) WKCrownSequencer *crownSequencer;
//watch 处于活跃 此方法可以用于界面不可见时,用来更新苹果表界面
- (void)willActivate; // Called when watch interface is active and able to be updated. Can be called when interface is not visible.
//watch不再处于活跃,不要做更新的工作
- (void)didDeactivate; // Called when watch interface is no longer active and cannot be updated.
//界面已经出现
- (void)didAppear WK_AVAILABLE_WATCHOS_ONLY(2.0); // Called when watch interface is visible to user
//界面将要消失
- (void)willDisappear WK_AVAILABLE_WATCHOS_ONLY(2.0); // Called when watch interface is about to no longer be visible
//不太明白这个方法干嘛的????
- (void)pickerDidFocus:(WKInterfacePicker *)picker WK_AVAILABLE_WATCHOS_ONLY(2.0);
- (void)pickerDidResignFocus:(WKInterfacePicker *)picker WK_AVAILABLE_WATCHOS_ONLY(2.0);
- (void)pickerDidSettle:(WKInterfacePicker *)picker WK_AVAILABLE_WATCHOS_ONLY(2.0);
//表格已经被选择
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex; // row selection if controller has WKInterfaceTable property
//不太明白这个方法干嘛的????
- (void)handleActionWithIdentifier:(nullable NSString *)identifier forNotification:(UNNotification *)notification WK_CLASS_AVAILABLE_IOS(10.0); // when the app is launched from a notification. If launched from app icon in notification UI, identifier will be empty
//不太明白这个方法干嘛的????
- (void)handleUserActivity:(nullable NSDictionary *)userInfo; // called on root controller(s) with user info
//设置控制器标题
- (void)setTitle:(nullable NSString *)title; // title of controller. displayed when controller active
//跳转到新的控制器
- (void)pushControllerWithName:(NSString *)name context:(nullable id)context; // context passed to child controller via initWithContext:
//返回上一个控制器
- (void)popController;
//返回到根控制器
- (void)popToRootController;
//重新加载根控制器
+ (void)reloadRootControllersWithNames:(NSArray<NSString*> *)names contexts:(nullable NSArray *)contexts;
//成为当前第一页
- (void)becomeCurrentPage;
//模态展示 控制器
- (void)presentControllerWithName:(NSString *)name context:(nullable id)context; // modal presentation
//模态展示 多个控制器
- (void)presentControllerWithNames:(NSArray<NSString*> *)names contexts:(nullable NSArray *)contexts; // modal presentation of paged controllers. contexts matched to controllers
//返回上一个控制器
- (void)dismissController;
//语音
//第一参数是 : 建议的 短语
//第二个是inputMode: 有三种 无图片,有图片和有动图
//第三方法是系统回调,当用户选择功能
- (void)presentTextInputControllerWithSuggestions:(nullable NSArray<NSString*> *)suggestions allowedInputMode:(WKTextInputMode)inputMode completion:(void(^)(NSArray * __nullable results))completion; // results is nil if cancelled
//语音
//第一参数是 : 建议的 短语,苹果表语言环境改变,会再次调用此Block,用此方法,和上面方法没有多大区别
//第二个是inputMode: 有三种 无图片,有图片和有动图
//第三方法是系统回调,当用户选择功能
- (void)presentTextInputControllerWithSuggestionsForLanguage:(NSArray * __nullable (^ __nullable)(NSString *inputLanguage))suggestionsHandler allowedInputMode:(WKTextInputMode)inputMode completion:(void(^)(NSArray * __nullable results))completion;
//语音界面消失
- (void)dismissTextInputController;
WKI_EXTERN NSString *const UIUserNotificationActionResponseTypedTextKey WK_AVAILABLE_WATCHOS_ONLY(2.0);
WKI_EXTERN NSString *const WKMediaPlayerControllerOptionsAutoplayKey WK_AVAILABLE_WATCHOS_ONLY(2.0); // NSNumber containing BOOL
WKI_EXTERN NSString *const WKMediaPlayerControllerOptionsStartTimeKey WK_AVAILABLE_WATCHOS_ONLY(2.0); // NSNumber containing NSTimeInterval
WKI_EXTERN NSString *const WKMediaPlayerControllerOptionsVideoGravityKey WK_AVAILABLE_WATCHOS_ONLY(2.0); // NSNumber containing WKVideoGravity
WKI_EXTERN NSString *const WKMediaPlayerControllerOptionsLoopsKey WK_AVAILABLE_WATCHOS_ONLY(2.0); // NSNumber containing BOOL
- (void)presentMediaPlayerControllerWithURL:(NSURL *)URL options:(nullable NSDictionary *)options completion:(void(^)(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable error))completion WK_AVAILABLE_WATCHOS_ONLY(2.0);
- (void)dismissMediaPlayerController WK_AVAILABLE_WATCHOS_ONLY(2.0);
WKI_EXTERN NSString *const WKAudioRecorderControllerOptionsActionTitleKey WK_AVAILABLE_WATCHOS_ONLY(2.0); // NSString (default is "Save")
WKI_EXTERN NSString *const WKAudioRecorderControllerOptionsAlwaysShowActionTitleKey WK_AVAILABLE_WATCHOS_ONLY(2.0); // NSNumber containing BOOL (default is NO)
WKI_EXTERN NSString *const WKAudioRecorderControllerOptionsAutorecordKey WK_AVAILABLE_WATCHOS_ONLY(2.0); // NSNumber containing BOOL (default is YES)
WKI_EXTERN NSString *const WKAudioRecorderControllerOptionsMaximumDurationKey WK_AVAILABLE_WATCHOS_ONLY(2.0); // NSNumber containing NSTimeInterval
- (void)presentAudioRecorderControllerWithOutputURL:(NSURL *)URL preset:(WKAudioRecorderPreset)preset options:(nullable NSDictionary *)options completion:(void (^)(BOOL didSave, NSError * __nullable error))completion WK_AVAILABLE_WATCHOS_ONLY(2.0);
- (void)dismissAudioRecorderController WK_AVAILABLE_WATCHOS_ONLY(2.0);
//干什么用的??????
- (nullable id)contextForSegueWithIdentifier:(NSString *)segueIdentifier;
- (nullable NSArray *)contextsForSegueWithIdentifier:(NSString *)segueIdentifier;
- (nullable id)contextForSegueWithIdentifier:(NSString *)segueIdentifier inTable:(WKInterfaceTable *)table rowIndex:(NSInteger)rowIndex;
- (nullable NSArray *)contextsForSegueWithIdentifier:(NSString *)segueIdentifier inTable:(WKInterfaceTable *)table rowIndex:(NSInteger)rowIndex;
//动画
- (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations WK_AVAILABLE_WATCHOS_ONLY(2.0);
//提示弹框
- (void)presentAlertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(WKAlertControllerStyle)preferredStyle actions:(NSArray <WKAlertAction *>*)actions WK_AVAILABLE_WATCHOS_ONLY(2.0);
//模态界面 增加用户通行证
- (void)presentAddPassesControllerWithPasses:(NSArray <PKPass *> *)passes completion:(void(^)(void))completion WK_AVAILABLE_WATCHOS_ONLY(2.0);
//消失通行证界面
- (void)dismissAddPassesController WK_AVAILABLE_WATCHOS_ONLY(2.0);
//通过代码 加menu 图片,所有的参数,一定不能为空
//第一个参数: UIImage
//第二个参数: menu 标题
//第三个参数:点击事件
- (void)addMenuItemWithImage:(UIImage *)image title:(NSString *)title action:(
SEL)action; // all parameters must be non-nil
//通过代码 加menu 图片
//第一个参数:图片名
//第二个参数: menu 标题
//第三个参数:点击事件
- (void)addMenuItemWithImageNamed:(NSString *)imageName title:(NSString *)title action:(SEL)action;
//通过代码 加menu 图片,所有的参数,一定不能为空
//第一个参数: 系统中自带的icon
//第二个参数: menu 标题
//第三个参数:点击事件
- (void)addMenuItemWithItemIcon:(WKMenuItemIcon)itemIcon title:(NSString *)title action:(SEL)action;
//通过代码清除所有的menu
- (void)clearAllMenuItems;
//干什么用的??????
- (void)updateUserActivity:(NSString *)type userInfo:(nullable NSDictionary *)userInfo webpageURL:(nullable NSURL *)webpageURL; // provide type and info to Handoff. userInfo and webpageUrl are passed to the application receiving the Handoff
- (void)invalidateUserActivity;
//干什么用的??????
+ (BOOL)openParentApplication:(NSDictionary *)userInfo reply:(nullable void(^)(NSDictionary * replyInfo, NSError * __nullable error)) reply WK_AVAILABLE_IOS_ONLY(8.2); // launches containing iOS application on the phone. userInfo must be non-nil
//开始 Glance更新
- (void)beginGlanceUpdates WK_AVAILABLE_WATCHOS_ONLY(2.0);
//结束Glance更新
- (void)endGlanceUpdates WK_AVAILABLE_WATCHOS_ONLY(2.0);
@end
如上的方法,也是第一次碰到,待深究后再更新,不足地方,望指正,感谢!!
网友评论