2015年9月10日,蘋果在新品發(fā)布會上宣布了3D-Touch功能。這是一種立體觸控技術(shù),屏幕可感應(yīng)不同的感壓力度觸控,很明顯這種技術(shù)給APP的交互又增添了樣式。Hann作為一名iOS開發(fā)者自然對這種新技術(shù)感到好奇,經(jīng)過一番的資料搜集與學(xué)習(xí),成功的將這種功能添加到正在開發(fā)的APP中。將3DTouch有關(guān)的知識點記錄下來以供自己查閱,也給有需求的開發(fā)人員提供一些參考。
3DTouch的使用
開發(fā)者應(yīng)該關(guān)注哪幾部分?
- 外部啟動標(biāo)簽(Home Screen Quick Actions)
- peek、pop以及action的使用
外部啟動標(biāo)簽(Home Screen Quick Actions)
外部啟動標(biāo)簽給快速使用APP的某些特定功能提供了便利,按壓具有3DTouch功能的APP圖標(biāo)可以激活標(biāo)簽。外部標(biāo)簽的添加上分為兩種:靜態(tài)添加標(biāo)簽、動態(tài)添加標(biāo)簽。靜態(tài)標(biāo)簽是直接在項目的info.plist配置,動態(tài)標(biāo)簽需要在AppDelegate啟動入口進(jìn)行添加。下面是兩種方式的添加,以及添加所涉及到的類、類型介紹。
靜態(tài)標(biāo)簽
上圖所示為靜態(tài)標(biāo)簽在info.plist中的配置內(nèi)容,即在plist中添加Array類型的UIApplicationShortcutItems以及字典類型的標(biāo)簽實體,下面對這些key和value進(jìn)行介紹:
// info.plist
UIApplicationShortcutItems[{
UIApplicationShortcutItemIconFile:圖標(biāo)名,(如果使用系統(tǒng)圖標(biāo)key為UIApplicationShortcutItemIconType)
UIApplicationShortcutItemTitle:標(biāo)簽名稱,(必須設(shè)置)
UIApplicationShortcutItemSubtitle:子標(biāo)簽名稱,
UIApplicationShortcutItemType:標(biāo)簽的ID,(必須設(shè)置)
{UIApplicationShortcutItemUserInfo[{key:value}...](附加信息)
}...]
動態(tài)標(biāo)簽
在添加動態(tài)標(biāo)簽之前我們先來了解幾個相關(guān)的類:
UIApplicationShortcutIcon標(biāo)簽圖標(biāo)類
@interface UIApplicationShortcutIcon : NSObject <NSCopying>
// 使用系統(tǒng)icon時用這個API創(chuàng)建UIApplicationShortcutIcon對象.
+ (instancetype)iconWithType:(UIApplicationShortcutIconType)type;
// 使用提供的圖片自定義UIApplicationShortcutIcon對象,templateImageName為圖片名.
+ (instancetype)iconWithTemplateImageName:(NSString *)templateImageName;
@end
UIApplicationShortcutItem標(biāo)簽類
@interface UIApplicationShortcutItem : NSObject <NSCopying, NSMutableCopying>
/**
* type: 標(biāo)簽ID
* localizedTitle: 標(biāo)簽名稱
* localizedSubtitle: 子標(biāo)題名稱
* icon: 標(biāo)簽圖標(biāo)對象
* userInfo: 附加信息
*/
- (instancetype)initWithType:(NSString *)type localizedTitle:(NSString *)localizedTitle localizedSubtitle:(nullable NSString *)localizedSubtitle icon:(nullable UIApplicationShortcutIcon *)icon userInfo:(nullable NSDictionary *)userInfo;
// 圖標(biāo)對象
@property (nullable, nonatomic, copy, readonly) UIApplicationShortcutIcon *icon;
// 對應(yīng)靜態(tài)標(biāo)簽的UIApplicationShortcutItemUserInfo.
@property (nullable, nonatomic, copy, readonly) NSDictionary<NSString *, id <NSSecureCoding>> *userInfo;
@end
在了解這兩個類后我們來看看動態(tài)添加標(biāo)簽的代碼:
// AppDelegate.m
// - (BOOL)application:didFinishLaunchingWithOptions:
// 動態(tài)添加快捷啟動
UIApplicationShortcutIcon *iconThree = [UIApplicationShortcutIcon iconWithTemplateImageName:@"showItemIconThree"];
UIApplicationShortcutItem *itemThree = [[UIApplicationShortcutItem alloc] initWithType:@"shortcutTypeThree" localizedTitle:@"動態(tài)標(biāo)簽" localizedSubtitle:nil icon:iconThree userInfo:nil];
[[UIApplication sharedApplication] setShortcutItems:@[itemThree]];
現(xiàn)在你可以使用靜態(tài)、動態(tài)以及混合方式創(chuàng)建快捷啟動標(biāo)簽,注意啟動標(biāo)簽最多可以有4個。下面就是我們創(chuàng)建的三個標(biāo)簽:
標(biāo)簽的點擊事件交互
// AppDelegate.m
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
BSDetailViewController *detailVC = [[BSDetailViewController alloc] init];
if ([shortcutItem.type isEqualToString:@"shortcutTypeOne"]) {
detailVC.navTitle = @"靜態(tài)標(biāo)簽一";
} else if ([shortcutItem.type isEqualToString:@"shortcutTypeTwo"]) {
detailVC.navTitle = @"靜態(tài)標(biāo)簽二";
} else if ([shortcutItem.type isEqualToString:@"shortcutTypeThree"]) {
detailVC.navTitle = @"動態(tài)標(biāo)簽";
}
[nav pushViewController:detailVC animated:YES];
}
到此外部啟動標(biāo)簽的創(chuàng)建以及處理事件都已經(jīng)介紹完了,如果看文章比較枯燥請配合demo源碼進(jìn)行查看.
peek、pop & action
在設(shè)置了具有peek、pop的頁面跟用戶交互的時候主要有三個階段:①輕按:提示用戶這個有預(yù)覽功能,即被選中的空間會凸顯出來周圍變得模糊。②加大力度:進(jìn)入peek預(yù)覽模式,如果設(shè)置了該預(yù)覽模式下的一些操作可以向上滑動顯示actions。③更大力度:激活pop,該階段一般是跳到指定的控制器。
輕按狀態(tài):

peek預(yù)覽:

actions:

pop激活:

要達(dá)到上述效果,在開發(fā)中都需要做什么呢?下面一步一步揭曉!
peek & pop
首先我們將能觸發(fā)peek、pop功能的控制器遵守UIViewControllerPreviewingDelegate協(xié)議
// BSTableViewController.m
@interface BSTableViewController()<UIViewControllerPreviewingDelegate,BSDetailViewControllerDelegate>
@end
然后注冊該控制器作為peek和pop預(yù)覽的代理以及提供預(yù)覽視圖容器
// BSTableViewController.m
// 重要
[self registerForPreviewingWithDelegate:self sourceView:self.view];
實現(xiàn)代理方法:
// BSTableViewController.m
// UIViewControllerPreviewingDelegate
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
self.selectedCell = [self searchCellWithPoint:location];
previewingContext.sourceRect = self.selectedCell.frame;
NSLog(@"peek");
BSDetailViewController *detailVC = [[BSDetailViewController alloc] init];
detailVC.delegate = self;
detailVC.navTitle = self.selectedCell.textLabel.text;
return detailVC;
}
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
NSLog(@"pop");
[self tableView:self.tableView didSelectRowAtIndexPath:[self.tableView indexPathForCell:self.selectedCell]];
}
actions
上面的一步操作使我們的主界面BSTableViewController有了輕按、peek以及pop的功能,還有個是action,這個action不屬于主界面而是詳情界面BSDetailViewController的事件,在demo里面,詳情將事件代理了主界面去處理事情。
BSDetailViewController生成事件方法:
// BSDetailViewController.m
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
//
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"刪除" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
if ([self.delegate respondsToSelector:@selector(detailViewController:DidSelectedDeleteItem:)]) {
[self.delegate detailViewController:self DidSelectedDeleteItem:self.navTitle];
}
}];
//
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"返回" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
if ([self.delegate respondsToSelector:@selector(detailViewControllerDidSelectedBackItem:)]) {
[self.delegate detailViewControllerDidSelectedBackItem:self];
}
}];
NSArray *actions = @[action1,action2];
return actions;
}
好了,上面將peek、pop以及action也介紹完了,看到這你應(yīng)該對3DTouch的使用有了一定的認(rèn)識,還想更深入的學(xué)習(xí)有關(guān)3Dtouch請查看官方文檔,想看本博客demo源碼的點擊我CoderHann進(jìn)行查看。